This is my first question, so I am sorry if I haven't made the problem/example clear!
I am trying to use ggplot2 to make a boxplot with compact letters displayed over each boxplot. I can achieve this when the plot is not faceted, however, when I split the plot with facet_grid, the levels that are not being used in each facet appear with no boxplot but just a letter. When I don't have the labels on the faceted plot, the empty levels do not appear.
Here is an example with a subset of my data:
# Dataframe structure
df <- structure(list(Var1 = c("A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "B", "B", "B", "B", "B", "B", "C", "C", "C",
"C", "C", "C"), Var2 = c("D", "E", "F", "G", "H", "K", "L", "M",
"N", "O", "P", "Q", "R", "W", "X", "Y", "Z", "AA", "BB", "CC",
"DD", "EE", "FF", "GG"), Freq = c(0.80150902033661, 0.781994333507093,
0.799306241892755, 0.792266624114348, 0.828044075178601, 0.848185700561263,
0.835649227456286, 0.801230470031423, 0.838080218109771, 0.799543499023431,
0.803041227907527, 0.843425895355166, 0.807979849825537, 0.831781069908856,
0.828458207462573, 0.846052247644906, 0.858664022176908, 0.82399256530848,
0.847625303767408, 0.799364138073169, 0.846301760577181, 0.801491930111703,
0.816622607179678, 0.848192570263525), Type = c("H.Tree", "H.Tree",
"H.Tree", "H.Tree", "H.Tree", "H.Tree", "H.Grass", "H.Grass",
"H.Grass", "H.Grass", "H.Grass", "H.Grass", "R.Tree", "R.Tree",
"R.Tree", "R.Tree", "R.Tree", "R.Tree", "R.Grass", "R.Grass",
"R.Grass", "R.Grass", "R.Grass", "R.Grass"), Time = c("Old",
"Old", "Old", "Old", "Old", "Old", "Old", "Old", "Old", "Old",
"Old", "Old", "New", "New", "New", "New", "New", "New", "New",
"New", "New", "New", "New", "New")), class = "data.frame", row.names = c(NA,
-24L))
df.labels <- structure(list(Type = c("R.Grass", "R.Tree", "H.Grass", "H.Tree"
), Letter = c("a", "b", "ab", "c")), class = "data.frame", row.names = c(NA, -4L))
# Boxplot not faceted
ggplot(df, aes(x=Type, y=Freq)) + geom_boxplot(fill="lightgrey", color = "black") +
geom_text(data=df.labels, aes(label = Letter, x = Type, y = 0.88))
# Faceted boxplot
ggplot(df, aes(x=Type, y=Freq)) + geom_boxplot(fill="lightgrey", color = "black") +
facet_grid(~Time, scales = "free_x")+
geom_text(data=df.labels, aes(label = Letter, x = Type, y = 0.88))
I thought perhaps adding inherit.aes in the geom_text section would work but didn't help out
geom_text(data=df.labels, aes(label = Letter, x = Type, y = 0.88), inherit.aes = FALSE)
Any suggestions would be much appreciated
As @27 ϕ 9 suggested, adding this to the label data frame works
df.labels$Time <- c("New", "New", "Old", "Old")