考虑以下代码:
library(ggplot2)
data = data.frame(x = c(1, 2,
3, 4,
5, 6),
label = c("foo", "bar",
"bar", "baz",
"baz", "boo"),
type = c(1, 1,
2, 2,
3, 3))
ggplot(data, aes(x = x, y = c(1))) +
labs(x = "", y = "") +
theme_bw() +
facet_wrap(~ type, ncol = 1, scales = "free_x") +
scale_x_discrete(aes(breaks = x, labels=label), limits = 1:6) +
geom_point()
它生成图像:
问题是我scale_x_discrete()
被忽略了。我希望每个构面的x比例在中显示标签data$label
,但仅在有数据的地方显示。换句话说,我想要这样的东西,但是在一张图表上:
我该怎么做?
您可能需要分别构建图,然后使用组合它们grid.arrange
:
library(ggplot2)
data = data.frame(x = c(1, 2,
3, 4,
5, 6),
label = c("foo", "bar",
"bar", "baz",
"baz", "boo"),
type = c(1, 1,
2, 2,
3, 3))
library(gridExtra) # Using V 2.0.0
p = list()
for(i in 1:3) {
df = subset(data, type == i)
p[[i]] = ggplot(df, aes(x = x, y = c(1)) ) +
labs(x = "", y = "") +
theme_bw() +
expand_limits(x = c(1, 6)) +
facet_wrap(~ type, ncol = 1) +
scale_x_continuous(breaks = df$x, labels = df$label) +
geom_point()
}
grid.arrange(grobs = p, ncol = 1)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句