我在R中使用ggplot用facet_wrap绘制几个条件。 我想把带有情节名称的条带放在右边的垂直轴上,而不是放在顶部。
这是一个例子:
library(ggplot2)
dat<- data.frame(name= rep(LETTERS[1:5], each= 4), value= rnorm(20), time= rep(1:5, 4))
gg<- ggplot(data= dat, aes(x= time, y= value)) +
geom_point() +
facet_wrap(~ name, ncol= 1)
这里的情节名称(A,B,C,D,E)位于顶部,我希望它们位于右侧,如下所示:
gg + facet_grid(name ~ .)
有一个简单的开关吗?
(我没有使用facet_grid
,因为我想使用nrow
附带的ncol
和facet_wrap
选项。
谢谢!达里奥
sessionInfo()
R version 3.0.1 (2013-05-16)
Platform: x86_64-apple-darwin10.8.0 (64-bit)
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggplot2_0.9.3.1
loaded via a namespace (and not attached):
[1] colorspace_1.2-4 dichromat_2.0-0 digest_0.6.4 grid_3.0.1
[5] gtable_0.1.2 labeling_0.2 MASS_7.3-29 munsell_0.4.2
[9] plyr_1.8.1 proto_0.3-10 RColorBrewer_1.0-5 Rcpp_0.11.0
[13] reshape2_1.2.2 scales_0.2.3 stringr_0.6.2 tools_3.0.1
答案 0 :(得分:3)
为了将来参考,您现在可以strip.position="right"
使用put them on the right side,例如:
library(ggplot2)
dat<- data.frame(name= rep(LETTERS[1:5], each= 4), value= rnorm(20), time= rep(1:5, 4))
gg<- ggplot(data= dat, aes(x= time, y= value)) +
geom_point() +
facet_wrap(~ name, ncol= 1, strip.position="right")
答案 1 :(得分:1)
如果您愿意在小平面的左侧使用小平面标签,那么有一个简单的解决方案,其中y轴成为x轴,x轴成为y轴。
library(ggplot2)
library(gridExtra)
library(gridGraphics)
# standard plot, facet labels on top
ggplot(diamonds) +
aes(x = carat, y = price) +
geom_point() +
facet_wrap( ~ cut)
# Use the gridExtra and gridGraphics utilities to rotate the plot.
# This requires some modifications to the axes as well. Note the use of
# a negative carat in the aes() call, and text modifications with theme()
grid.newpage()
pushViewport(viewport(angle = 90))
grid.draw(ggplotGrob(
ggplot(diamonds) +
aes(x = price, y = -carat) +
geom_point() +
facet_wrap( ~ cut) +
scale_y_continuous(name = "Carat", breaks = -seq(0, 5, by = 1), labels = seq(0, 5, by = 1)) +
theme(axis.text.y = element_text(angle = 270),
axis.title.y = element_text(angle = 270),
axis.text.x = element_text(angle = 270))
))
将facet_wrap
标签移动到旋转图形的右侧,旋转角度为-90
。但是,这将在图上方具有有效的x轴。你需要处理代码,将y轴标签从“标准”图的左侧移动到右侧,然后按-90
所示旋转。