wilkelab / cowplot

cowplot: Streamlined Plot Theme and Plot Annotations for ggplot2
https://wilkelab.org/cowplot/
704 stars 84 forks source link

geom_text not scaling well #132

Closed ghost closed 5 years ago

ghost commented 5 years ago

Hello, I am facing a strange issue in Rstudio with cowplot. I am drawing barplots and when I display the value on each bar on the top of the bars, these numbers are out of frame in the resulting grid drawn with cowplot. Interestingly, I am usint a R markdown, if I click to expand the plot, the scaling is done properly. However, in the working document it's not, and it is not either when knitting the code into pdf or html. Here is my code

{r , tidy = TRUE, linewidth=60}
library(ggplot2)
library(devtools)
dev_mode(on=T)
library(cowplot)
library(viridis)
library(dplyr)
ksize = c("k63", "k101", "k201", "reference")
totsize = c(9460388, 10782564, 11771990, 9273909)
N50 = c(133323, 136961, 149886, 9273909)
a = data.frame(K = ksize, size = totsize, n50 = N50)
assembly_size = ggplot(data = a, aes(x = K, y = size)) + geom_bar(stat = "identity", fill = plasma(4)) + scale_x_discrete(limits = c("reference", "k63", "k101", "k201")) + ggtitle("assembly size") + geom_text(aes(label=size), position=position_dodge(width=0.9), vjust=-0.25)
b = filter(a, K != "reference")
N50_size =  ggplot(data = b, aes(x = K, y = n50)) + geom_bar(stat = "identity", fill = plasma(3)) + scale_x_discrete(limits = c("k63", "k101", "k201")) + ggtitle("N50") + geom_text(aes(label=n50), position=position_dodge(width=0.9), vjust=-0.25)
totsizedbg = c(4614422, 4626530, 4659579)
N50dbg = c(67344, 82868, 95739)
namedbg = c("dbg63", "dbg101", "dbg201")
c = data.frame(dbg = namedbg, size = totsizedbg, n50 = N50dbg)
dbg_size = ggplot(data = c, aes(x = dbg, y = totsizedbg)) + geom_bar(stat = "identity", fill = plasma(3)) + scale_x_discrete(limits = c("dbg63", "dbg101", "dbg201")) + ggtitle("dbg size") + geom_text(aes(label=totsizedbg), position=position_dodge(width=0.9), vjust=-0.25)
dbg_n50 = ggplot(data = c, aes(x = dbg, y = N50dbg)) + geom_bar(stat = "identity", fill = plasma(3)) + scale_x_discrete(limits = c("dbg63", "dbg101", "dbg201")) + ggtitle("dbg N50") + geom_text(aes(label=N50dbg), position=position_dodge(width=0.9), vjust=-0.25)

plot_grid(assembly_size, N50_size, dbg_size, dbg_n50, nrow = 2, ncol = 2, labels = "", align = "hv")

Here is how the output looks like in the working document and in the knitted output cowplot_scaling

And here how it looks like when "zooming in" using the "show in new window" in Rstudio. Notice now it's all right. Unfortunately it will not look like this when knitted in html or pdf cowplot_scaling2

Thank you

clauswilke commented 5 years ago

In the R Markdown document, the font size you're using is too large for the figure size you are using. Either the figure needs to be made bigger or the font size smaller. For more help, you could ask this question on stackoverflow.com or the R Studio forums. It is not a bug in cowplot, and I will close this issue.