Reputation: 379
How can I shift the facet labels (A, B, C) so that they are inside the boxes, in the upper lefthand corner?
Sample dataset:
date <- as.factor(rep(c(1,2,3,4,5,6,7,8), 7))
chlconc <- runif(56, min = 0.1, max = 15.9)
colony <- as.factor(c(rep("A", 24), rep("B", 16), rep("C", 16)))
year <- as.factor(c(rep("2014", 8), rep("2016", 8), rep("2017", 8), rep("2016", 8), rep("2017", 8), rep("2014", 8), rep("2017", 8)))
graphvalues <- data.frame(year, date, colony, chlconc)
ggplot code
library(ggplot2)
library(ggthemes)
pd <- position_dodge(0.1)
ggplot(graphvalues, aes(date, chlconc, group = year)) +
facet_wrap(~colony, ncol = 1, labeller = label_parsed) +
geom_line(aes(color = year), position = pd) +
geom_point(aes(color = year), position = pd) +
scale_y_continuous(limit=c(0, 16), breaks=c(0, 4, 8, 12, 16), labels=c(0, 4, 8, 12, 16)) +
scale_color_discrete(name="Year") +
geom_hline(yintercept=2, linetype="dashed") +
theme(legend.text=element_text(size=14)) +
theme(axis.text=element_text(size=14)) +
theme(axis.title=element_text(size=18)) +
theme(legend.title=element_text(size=14)) +
theme_few() +
theme(strip.text = element_text(hjust = 0, size = 14, face = "bold"))
Upvotes: 3
Views: 453
Reputation: 1580
Facet labels live outside of the plot area, I don't think there is a way to make them go inside your plot. But you can get labels inside the plot area by adding a geom_text()
layer with your facetting variable as the label, and setting x and y values for the geom_text()
layer that will place it in the upper left of your graph. Then you also remove the facet labels.
ggplot(graphvalues, aes(date, chlconc, group = year)) +
facet_wrap(~colony, ncol = 1) +
geom_line(aes(color = year), position = pd) +
geom_point(aes(color = year), position = pd) +
scale_y_continuous(limit=c(0, 16), breaks=c(0, 4, 8, 12, 16), labels=c(0, 4, 8, 12, 16)) +
scale_color_discrete(name="Year") +
geom_hline(yintercept=2, linetype="dashed") +
theme_classic() +
theme(strip.text = element_blank(), #remove strip text
strip.background = element_blank()) + #remove strip rectangles
geom_text(aes(label = colony, x = 0.75, y = 15.5)) #add titles using geom_text()
Upvotes: 3