R/ggplot:带有 facet_wrap 的竖条文本
R/ggplot: Vertical strip text with facet_wrap
我在 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
,因为我想使用 facet_wrap
附带的选项 nrow
和 ncol
。
谢谢!达里奥
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
如果您愿意在刻面的左侧放置刻面标签,那么有一个简单的解决方案,其中 y-axis 变为 x-axis,x-axis 变为y-axis.
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-axis 。您需要编写代码,将 y-axis 标签从 "standard" 图的左侧移动到右侧,然后按照 -90
.
所示进行旋转
为了将来参考,您现在可以使用 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")
我在 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)
gg + facet_grid(name ~ .)
是否有一个简单的开关可以做到这一点?
(我没有使用 facet_grid
,因为我想使用 facet_wrap
附带的选项 nrow
和 ncol
。
谢谢!达里奥
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
如果您愿意在刻面的左侧放置刻面标签,那么有一个简单的解决方案,其中 y-axis 变为 x-axis,x-axis 变为y-axis.
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-axis 。您需要编写代码,将 y-axis 标签从 "standard" 图的左侧移动到右侧,然后按照 -90
.
为了将来参考,您现在可以使用 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")