我正在尝试使用facet_wrap创建一个图表,在每个包装的方面中都有一个facet_grid,但我无法做到。有什么建议吗?
举个例子,如果我要对两个数量的月平均值进行年度比较,我想要-
我能来的最接近的就是这个,
library(ggplot2)
# create dataset
df <- data.frame(
Facet1 = rep(c(1,2,3),24),
Facet2 = c(rep(1,24),rep(2,24)),
Year = rep(c(rep(2012,12),rep(2013,12)),2),
Month = rep(rep(1:12,2),2),
ValueX = sample(0:5,144,replace = TRUE),
ValueY = sample(0:5,144,replace = TRUE)
)
df <- df[!(df$Facet1 == 2 & df$Facet2 == 2),]
ggplot(df, aes(ValueX, ValueY)) + geom_point() +
facet_grid(Facet2 + Year ~ Month)

然而,我理想中想要的是类似于此的东西(在我看来,类似于ggplot() ... + facet_grid(Year ~ Month) + facet_wrap(Facet2~.)) -

PS:我认为后者的面要清晰得多,更整洁。评论?有其他选择吗?
发布于 2013-11-22 17:19:04
也许我误解了你想要做的事情,但这不是你想要的吗?
ggplot(df, aes(ValueX, ValueY)) + geom_point() +
facet_grid(Facet2 ~ Facet1)如果您想要更改facet标题以匹配您的示例,请查看facet_grid()的facet_grid()参数。
发布于 2019-02-19 10:38:52
这可以很容易地使用cowplot完成。
plot1<-ggplot(df[df$Facet2==1,], aes(ValueX, ValueY)) + geom_point() +
facet_grid(Year ~ Month)+
theme_bw()
plot2<-ggplot(df[df$Facet2==2,], aes(ValueX, ValueY)) + geom_point() +
facet_grid(Year ~ Month)+
theme_bw()
plot_grid(plot1, plot2, labels = c("1", "2"), nrow = 2)

发布于 2013-11-22 18:00:55
我不确定你想要做什么,但我认为在这里用格`更容易得到你想要的东西:
library(latticeExtra)
xyplot(ValueY~ValueX|Facet1+Facet2,data=df,
between=list(x=2,y=0),
par.settings = ggplot2like(),axis=axis.grid)

https://stackoverflow.com/questions/20150473
复制相似问题