Reputation: 154
I'm working with ggplot
and I love the facet_wrap
functionality - it's an absolute necessity given the dimensionality of my data and my figures. However, I find myself unable to customize the plots to the degree I want using facet_wrap
. Basically, I'd like to place the facet_wrap
labels inside of the plot area - preferably in the top center-right of each sub-plot, as shown in the attached picture. Unfortunately I can't find any formatting options for facet_wrap
other than strip.position
, which doesn't seem to allow for an in-plot placement. strip.placement
allows for an 'inside' option, but that refers to 'inside the axis', not inside the plot area itself.
I'm concerned the answer may not lie in facet_wrap
at all. I've previously used ggarrange
and its customizable labels for similar plots in the past, but given the data I'm working with, I'd like to keep the single step functionality of facet_wrap
. Has anyone dealt with this problem before? I've attached sample data and code below.
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)")
Upvotes: 1
Views: 1585
Reputation: 31452
You can eliminate the facet strips using element_blank
and put the labels in directly as text:
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())
Upvotes: 2