I have the following data frame:
structure(list(levels = c("Low", "Medium", "High", "Low", "Medium",
"High", "Low", "Medium", "High", "Low", "Medium", "High", "Low",
"Medium", "High", "Low", "Medium", "High", "Low", "Medium", "High",
"Low", "Medium", "High", "", "", "", "", "", "", "", "", "",
"", "", ""), days = c(3.4, 3.4, 3.4, 5.2, 5.2, 5.2, 5.9, 5.9,
5.9, 7, 7, 7, 3.4, 3.4, 3.4, 5.2, 5.2, 5.2, 5.9, 5.9, 5.9, 7,
7, 7, 3.4, 3.4, 3.4, 5.2, 5.2, 5.2, 5.9, 5.9, 5.9, 7, 7, 7),
categories = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("0",
"0.5", "1"), class = "factor"), fit = c(0.006681428, 0.006604253,
0.006527963, 0.010609297, 0.005799118, 0.003162868, 0.012692178,
0.00551306, 0.002384881, 0.016807205, 0.005091636, 0.001529772,
0.0125202, 0.009006417, 0.00647231, 0.014553106, 0.00902407,
0.005583731, 0.015428702, 0.009030945, 0.005271914, 0.016910442,
0.009041758, 0.004816553, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA), se = c(0.004542971, 0.001805012, 0.00480149,
0.00471013, 0.001326741, 0.001551987, 0.004750372, 0.001205801,
0.001008321, 0.005366604, 0.001086265, 0.000581074, 0.004243848,
0.001786382, 0.00233388, 0.003867866, 0.001699203, 0.001569163,
0.00382492, 0.001681837, 0.001383752, 0.004003663, 0.001674544,
0.001219947, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA), lower = c(0.00175533, 0.003862174, 0.001537301, 0.004430336,
0.003701695, 0.001207622, 0.00607794, 0.003589446, 0.001040653,
0.00896413, 0.003350344, 0.000726369, 0.006428368, 0.006101782,
0.003188264, 0.008629558, 0.006235693, 0.003216664, 0.009475882,
0.006265836, 0.003149848, 0.010615311, 0.006286038, 0.00293036,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), upper = c(0.025084575,
0.011271135, 0.027277596, 0.025187986, 0.009074141, 0.008257659,
0.026313711, 0.008458803, 0.00545599, 0.031295798, 0.007730918,
0.003218919, 0.024244082, 0.013275278, 0.013094624, 0.024442476,
0.013042945, 0.009675756, 0.025026645, 0.013000329, 0.008810988,
0.026837471, 0.012989758, 0.007907206, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA), time = c("1 day", "1 day", "1 day",
"2 days", "2 days", "2 days", "3 days", "3 days", "3 days",
"4 days", "4 days", "4 days", "1 day", "1 day", "1 day",
"2 days", "2 days", "2 days", "3 days", "3 days", "3 days",
"4 days", "4 days", "4 days", "1 day", "1 day", "1 day",
"2 days", "2 days", "2 days", "3 days", "3 days", "3 days",
"4 days", "4 days", "4 days"), time_labels = structure(c(1L,
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 2L,
2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L,
3L, 3L, 4L, 4L, 4L), .Label = c("1 day", "2 days", "3 days",
"4 days"), class = "factor")), row.names = c(NA, -36L), class = "data.frame")
And I created the following ggplot:
e.df$time_labels <- factor(e.df$time, levels=c("1 day", "2 days", "3 days", "4 days"))
e.df$categories <- as.factor(e.df$categories)
xy_labs <- e.df %>%
group_by(levels) %>%
dplyr::summarise(pos = which.max(time_labels),
x = time_labels[pos],
y = fit[pos]) %>%
filter(!is.na(y))
g2 <- ggplot(e.df,aes(x=time,y=fit,color=levels,ymin=lower,ymax=upper)) geom_point(position = position_dodge(width = 0.2)) geom_line(aes(group = levels), linetype = "dotted")
geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2, position = position_dodge(width = 0.2))
geom_label_repel(data=xy_labs, aes(x=x, y=y, label = levels, color = levels),
nudge_x = 0.1, inherit.aes=F,
na.rm = FALSE) theme_bw() theme(legend.position = "none") facet_wrap(~ categories) scale_colour_manual(values = c("#009E73", "#0072B2", "#CC79A7", "#cacaca")) ylim(0, 0.045)
plot(g2)
I end up with a graph that has labels on the third facet (categories == 1). How do I remove those labels? I want a blank graph for the third facet. Eventually, I want to be able to show the lines one at a time. I'm creating this for a presentation.
CodePudding user response:
As you want labels per facet it looks more appropriate to me to include categories
in the group_by
when creating the labels dataset. This way the labels are placed at the correct position and as a side-effect that will automatically remove the labels from the third panel:
library(ggplot2)
library(ggrepel)
library(dplyr)
xy_labs <- e.df %>%
group_by(levels, categories) %>%
dplyr::summarise(
pos = which.max(time_labels),
x = time_labels[pos],
y = fit[pos]
) %>%
filter(!is.na(y))
ggplot(e.df, aes(x = time, y = fit, color = levels, ymin = lower, ymax = upper))
geom_point(position = position_dodge(width = 0.2))
geom_line(aes(group = levels), linetype = "dotted")
geom_errorbar(aes(ymin = lower, ymax = upper),
width = 0.2,
position = position_dodge(width = 0.2)
)
geom_label_repel(
data = xy_labs, aes(x = x, y = y, label = levels, color = levels),
nudge_x = 0.1, inherit.aes = F,
na.rm = FALSE
)
theme_bw()
theme(legend.position = "none")
facet_wrap(~categories)
scale_colour_manual(values = c("#009E73", "#0072B2", "#CC79A7", "#cacaca"))
ylim(0, 0.045)