如何更改ggplot中Facet_Wrap标签的位置?

时间:2019-05-20 18:15:04

标签: r ggplot2

我正在使用ggplot,我喜欢facet_wrap功能-鉴于我的数据和图形的维度,这是绝对必要的。但是,我发现自己无法使用facet_wrap将图自定义到我想要的程度。基本上,我想将facet_wrap标签放置在绘图区域内-最好放在每个子绘图的右上角,如所附图片所示。不幸的是,除了facet_wrap之外,我找不到strip.position的任何格式设置选项,这似乎不允许在打印中放置。 strip.placement允许使用“内部”选项,但这是指“在轴内部”,而不是在绘图区域本身内部。

我担心答案可能根本就不在facet_wrap中。过去,我以前曾将ggarrange及其可自定义的标签用于类似的绘图,但是鉴于我正在使用的数据,我想保留facet_wrap的单步功能。有人处理过这个问题吗?我在下面附加了示例数据和代码。

structure(list(Ocean.Regions = c("Western Tropical Pacific", 
"South Pacific", "South Atlantic", "Southern Ocean", "Western Tropical Pacific", 
"South Pacific", "South Atlantic", "Southern Ocean", "Western Tropical Pacific", 
"South Pacific", "South Atlantic", "Southern Ocean", "Western Tropical Pacific", 
"South Atlantic", "South Pacific", "Southern Ocean", "Western Tropical Pacific", 
"South Atlantic", "South Pacific", "Southern Ocean", "Western Tropical Pacific", 
"South Atlantic", "South Pacific", "Southern Ocean", "Western Tropical Pacific", 
"South Pacific", "South Atlantic", "Southern Ocean", "Western Tropical Pacific", 
"South Atlantic", "South Pacific", "Southern Ocean", "Western Tropical Pacific", 
"South Pacific", "South Atlantic", "Southern Ocean", "Western Tropical Pacific", 
"South Atlantic", "South Pacific", "Southern Ocean", "Western Tropical Pacific", 
"South Pacific", "South Atlantic", "Southern Ocean", "Western Tropical Pacific", 
"South Atlantic", "South Pacific", "Southern Ocean", "Western Tropical Pacific", 
"South Pacific"), binned_alt = c(0, 0, 0, 0, 1000, 1000, 1000, 
1000, 2000, 2000, 2000, 2000, 3000, 3000, 3000, 3000, 4000, 4000, 
4000, 4000, 5000, 5000, 5000, 5000, 6000, 6000, 6000, 6000, 7000, 
7000, 7000, 7000, 8000, 8000, 8000, 8000, 9000, 9000, 9000, 9000, 
10000, 10000, 10000, 10000, 11000, 11000, 11000, 11000, 12000, 
12000), `25%` = c(19.1, 23.95, 14.5, 9.3, 14.9, 12.45, 6.6, 5.9, 
9.15, 5.675, 1.35278884462151, 5.25, 6.3, 4.9, 5.5, 3.3, 5.45, 
3.4, 5.275, 2.375, 5, 3.3, 4.6, 0.688259109311741, 0.561264822134387, 
4.35, 3.575, 0.376811594202899, 0.561264822134387, 3.2, 3.475, 
0.376811594202899, 0.561264822134387, 0.376811594202899, 2.825, 
0.376811594202899, 0.561264822134387, 1.55278884462151, 0.688259109311741, 
0.376811594202899, 0.557971014492754, 4.15, 6.3, 0.376811594202899, 
0.557971014492754, 0.605577689243028, 0.688259109311741, 0.376811594202899, 
0.557971014492754, 0.688259109311741), `50%` = c(24.9, 28.45, 
23.05, 10.1, 18, 17, 9.65, 7.8, 11.7, 9.85, 2.1, 5.7, 8.7, 5.1, 
7.25, 5.4, 7.05, 4.5, 5.65, 3.3, 6.6, 3.7, 7.15, 2.3, 3.65, 6, 
4.45, 0.53253535175732, 3.1, 4.65, 4.75, 0.688259109311741, 0.561264822134387, 
2.8, 9.05, 0.376811594202899, 0.561264822134387, 3.2, 3.5, 0.376811594202899, 
0.561264822134387, 7.5, 14.5, 0.376811594202899, 0.561264822134387, 
0.605577689243028, 0.688259109311741, 0.376811594202899, 0.557971014492754, 
0.688259109311741), `75%` = c(28.3, 33.9, 34.275, 10.8, 23.55, 
19.1, 13.175, 9, 15.85, 16.2, 7.9, 7.775, 11.8, 5.6, 10.3, 6.1, 
9.375, 5.75, 7.175, 5.725, 7.6, 6.45, 7.425, 3.4, 6.325, 7.6, 
6.225, 1.775, 5.1, 7.675, 5.875, 0.688259109311741, 2.925, 5, 
15.725, 1.5, 0.561264822134387, 5.45, 9, 0.605577689243028, 3.6, 
9.675, 23.55, 0.376811594202899, 3.625, 0.605577689243028, 1.69412955465587, 
0.688259109311741, 0.557971014492754, 0.688259109311741)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -50L))

library(ggplot)
library(ggthemes)
test.plot <- ggplot(SOverflowTest, [![enter image description here][1]][1]aes(y = binned_alt/1000, x = `50%`)) +
  geom_path(size = 1.2) + #geom_point() +
  theme_tufte(base_size = 22) +
  geom_errorbarh(aes(xmin =`25%`, xmax = `75%`), height = 0) + 
  theme(axis.title.x = element_text(vjust=-0.5),
        axis.title.y = element_text(vjust=1.5),
        panel.grid.major = element_line(colour = "grey80"),
        axis.line = element_line(size = 0.5, colour = "black")) +
  facet_wrap(~Ocean.Regions, ncol = 2, as.table = T) +
  xlab("MEK (ppt)") +
  ylab("Altitude (km)")

1 个答案:

答案 0 :(得分:1)

您可以使用element_blank消除刻面条纹,并将标签直接作为文本放置:

library(ggthemes)
ggplot(SOverflowTest, aes(y = binned_alt/1000, x = `50%`)) +
  geom_path(size = 1.2) + #geom_point() +
  theme_tufte(base_size = 22) +
  geom_errorbarh(aes(xmin =`25%`, xmax = `75%`), height = 0) + 
  facet_wrap(~Ocean.Regions, ncol = 2, as.table = T) +
  xlab("MEK (ppt)") +
  ylab("Altitude (km)") +
  geom_text(aes(label = Ocean.Regions, x=18, y=12), 
    data = data.frame(Ocean.Regions = unique(SOverflowTest$Ocean.Regions))) +
  theme(
    axis.title.x = element_text(vjust=-0.5),
    axis.title.y = element_text(vjust=1.5),
    panel.grid.major = element_line(colour = "grey80"),
    axis.line = element_line(size = 0.5, colour = "black"),
    strip.text = element_blank())

enter image description here