我正在寻找一些帮助来创建带有角度 x 轴刻度标签的多面图,这可能最好通过以下示例来解释:
require(ggplot2)
df <- data.frame(group=factor(c('sex','sex','race','race')), variable=c('Female','Male','White','African American'), value=1:4)
p <- ggplot(aes(x=variable, y=value), data=df)
p <- p + geom_line()
p <- p + facet_grid(. ~ group, scale="free")
p <- p + opts(axis.text.x=theme_text(angle=45,hjust=1,vjust=1))
ggsave(p, file='faceted.pdf', width=6, height=4)
这产生了这个数字,其中右侧的 x-ticks 未对齐:
好像是使用
scale="free"
时引入的问题在 facet_grid
并且与变化的刻度标签长度有关。非常感谢任何建议。
最佳答案
看起来像一个错误:https://github.com/hadley/ggplot2/issues/221这显然已在 ggplot2 0.9 及更高版本中得到修复。
> help(package='ggplot2')
Information on package ‘ggplot2’
Description:
Package: ggplot2
Type: Package
Title: An implementation of the Grammar of Graphics
Version: 0.8.9
(对我来说太糟糕了!)
错误可重现:
qplot(reorder(model, hwy), hwy, data=mpg) +
facet_grid(. ~ manufacturer, scales="free") +
opts(axis.text.x = theme_text(angle=90))
关于r - 多面 ggplot 图形中成角度的 x 轴刻度的正确垂直对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8934529/