I am using facet_wrap and need to move the x axis table left and the y2 axis lable up to make room for my legend and was wondering if the is possible.
Below is my code and an image of what I am trying to achieve.
p <- ggplot(data_Long, aes(x = Week, y = count)) +
geom_line(aes(colour = Temp, linetype = Water)) +
labs(x = "Germination weeks", y = "% Germination", colour = "Temperature (°C)",
linetype = "Water Availibility") +
ylim(0, 100) +
theme_classic() +
theme(
strip.text.x = element_text(size=10, face="italic"), legend.position = c(0.85,0.15))+
scale_y_continuous( limits=c(0, 100),
sec.axis = sec_axis(~ .,
breaks = c(25, 50, 75),
labels = c("5/15", "10/25", "15/40"),
name = "Temperature (°C)")) +
geom_segment(aes(x = 0, y = 75, xend = 8, yend = 75), colour = "grey", size=0.2) +
geom_segment(aes(x = 8, y = 50, xend = 12, yend = 50), colour = "grey", size=0.2) +
geom_segment(aes(x = 12, y = 25, xend = 24, yend = 25), colour = "grey", size=0.2) +
geom_segment(aes(x = 24, y = 50, xend = 26, yend = 50), colour = "grey", size=0.2) +
geom_segment(aes(x = 8, y = 75, xend = 8, yend = 50), colour = "grey", size=0.2) +
geom_segment(aes(x = 12, y = 50, xend = 12, yend = 25), colour = "grey", size=0.2) +
geom_segment(aes(x = 24, y = 25, xend = 24, yend = 50), colour = "grey", size=0.2) +
annotate('rect', xmin=13, xmax=24, ymin=0, ymax=25, alpha=.1, fill='blue')+ ######Win1
annotate('rect', xmin=7, xmax=7.2, ymin=0, ymax=75, alpha=.1, fill='blue')+ #####Wet 1
annotate('rect', xmin=9, xmax=9.2, ymin=0, ymax=50, alpha=.1, fill='blue') + #####Wet 2
annotate('rect', xmin=11, xmax=11.2, ymin=0, ymax=50, alpha=.1, fill='blue') + #####Wet 3
facet_wrap(~Species, ncol = 2)+
scale_color_manual(values=c("royalblue", "orange", "forestgreen", "firebrick3"))+
scale_x_continuous(breaks=c(0, 5, 10, 15, 20, 25 ))
p
As started by @Edward's comment, use axis.title.*
theme arguments.
ggplot(mtcars, aes(mpg, disp)) +
facet_wrap(cyl ~ ., ncol = 2) +
geom_point(aes(color = factor(gear))) +
scale_y_continuous(sec.axis = sec_axis(transform = ~ ., name = "Quux")) +
theme(
legend.position = "inside",
legend.position.inside = c(0.85, 0.15),
axis.title.x = element_text(hjust = 0.25),
axis.title.y.right = element_text(hjust = 0.25)
)
FYI, with ggplot2_3.5.0
or newer, using legend.position=c(0.85, 0.15)
results in a warning:
Warning: A numeric `legend.position` argument in `theme()` was deprecated in ggplot2 3.5.0.
ℹ Please use the `legend.position.inside` argument of `theme()` instead.
I demonstrated the preferred method above.