我正在通过base_size更改theme_set。当我在屏幕上查看结果图时,它看起来很棒。但是,当我将其另存为pdf时,x轴标签与轴号有点太近了。

一件事:

 theme_set(theme_bw(base_size = 9))

不会造成任何问题,但是
 theme_grey(theme_bw(base_size = 9))

做。这是一个示例图:

R代码
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()

最佳答案

我认为pdf可能是一个问题。将ggsavevjust = -0.5结合使用对我来说很有效。我将用下面的代码替换代码的最后三行

ggsave('figure3.pdf', g + opts(axis.title.x = theme_text(vjust = -0.5)),
   width = 7.5, height = 3.5)

这是输出。

关于r - 在ggplot2中设置base_size时,轴标签重叠,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7567216/

10-12 17:12