Overlapping axis labels when setting base_size in ggplot2
I'm changing base_size
via theme_set
. When I view the resulting plot on screen, it looks great. However, when I save it as a pdf, the x-axis label is a bit too close to th开发者_开发百科e axis numbers.
One small thing:
theme_set(theme_bw(base_size = 9))
doesn't cause any problems but
theme_grey(theme_bw(base_size = 9))
does. Here is an example graph:
R Code
require(ggplot2)
theme_set(theme_bw(base_size = 9))
#Data
m = c(0.475, 0.491, 0.4800, 0.4318, 0.4797, 0.5718)
m = c(m, 0.00252, 0.00228, 0.00254, 0.00291, 0.00247, 0.00201)
m = c(m, 0.306, 0.260, 0.3067, 0.3471, 0.3073, 0.2357)
s = c(0.0172, 0.0681, 0.0163, 0.0608, 0.0170, 0.1088)
s = c(s, 0.000087, 0.000367, 0.000091, 0.000417, 0.000094, 0.000417)
s = c(s, 0.0092, 0.0447, 0.0110, 0.0593, 0.0113, 0.0504)
df = data.frame(m=m, s=s)
df$data_set = as.factor(c("Data set 1", "Data set 2"))
df$est = factor(rep(c("A", "B", "C"), each=2))
df$par = rep(c("c1", "c2", "c3"), each=6)
g = ggplot(data =df, aes(y=est, x=m)) +
geom_point() +
geom_errorbarh(aes(xmax = m + 2*s, xmin = m-2*s), width=0.1) +
facet_grid(data_set~par, scales="free_x") +
xlab("Parameter value") + ylab("")
g
pdf("figure3.pdf", width=7.5, height=3.5)
print(g)
dev.off()
By default ggplot2 make axis titles so close to axis text. A trick is to insert a newline character \n
in the string
xlab("\nParameter value") + ylab("")
I think it could be an issue with pdf
. Using ggsave
with vjust = -0.5
worked for me. I would replace the last three lines of your code with
ggsave('figure3.pdf', g + opts(axis.title.x = theme_text(vjust = -0.5)),
width = 7.5, height = 3.5)
Here is the output.
精彩评论