I have a dataset with SUD treatment rates for each state for each year from 2008 to 2017. Two states received an intervention and the rest did not. I am able to plot the treatment rates for each intervention state as a separate line and the non-intervention states (grouped as one line using the mean) on the same graph.
I would like to include the grouped line in the legend, but am having trouble doing so. So far only the intervention states are labeled. I've pasted a subset of the data (not exactly what the plot shows, but provides insight into the structure) and provided the ggplot code with the resulting figure. Any help would be appreciated. Thank you!
structure(list(statename = c("Alabama", "Alabama", "Alabama",
"Alabama", "Alabama", "Alabama", "Alabama", "Alabama", "Alabama",
"Alabama", "Colorado", "Colorado", "Colorado", "Colorado", "Colorado",
"Colorado", "Colorado", "Colorado", "Colorado", "Colorado", "Iowa",
"Iowa", "Iowa", "Iowa", "Iowa", "Iowa", "Iowa", "Iowa", "Iowa",
"Iowa", "Washington", "Washington", "Washington", "Washington",
"Washington", "Washington", "Washington", "Washington", "Washington",
"Washington"), YEAR = c(2008L, 2009L, 2010L, 2011L, 2012L, 2013L,
2014L, 2015L, 2016L, 2017L, 2008L, 2009L, 2010L, 2011L, 2012L,
2013L, 2014L, 2015L, 2016L, 2017L, 2008L, 2009L, 2010L, 2011L,
2012L, 2013L, 2014L, 2015L, 2016L, 2017L, 2008L, 2009L, 2010L,
2011L, 2012L, 2013L, 2014L, 2015L, 2016L, 2017L), RML_ever_state = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L), TotalAdolescent_noprior_Admissionrate = c(4.07959747971533,
17.1095315089946, 28.605586683907, 11.488423601983, 14.9233623164375,
13.0325736612464, 11.5686458431041, 15.2887154594351, 18.9275946907701,
17.9164783752891, 28.9653195613967, 30.542173819128, 33.8611241088185,
29.9657748758525, 25.214146698236, 22.847991066509, 21.975843495247,
21.783383749025, 25.8868468603421, 23.6029880132029, 45.9863203727017,
51.6710909784629, 61.713238062499, 48.3292305542656, 52.4339028740025,
54.9353761700907, 50.2282357945714, 46.8358255273624, 63.8611763171622,
63.7710085311979, 50.5294974023959, 53.0407358054702, 54.7486826331652,
59.981622172154, 57.2193036593259, 54.5478493207391, 49.924220486418,
41.9396870928129, 11.3163228651143, 0.258492875751707)), row.names = c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 31L, 32L, 33L, 34L, 35L,
36L, 37L, 38L, 39L, 40L, 117L, 118L, 119L, 120L, 121L, 122L,
123L, 124L, 125L, 126L, 387L, 388L, 389L, 390L, 391L, 392L, 393L,
394L, 395L, 396L), class = "data.frame")
ggplot(vars1, aes(x = YEAR, y = TotalAdolescent_noprior_Admissionrate, group = statename)) +
stat_summary(
data = ~ subset(., RML_ever_state == 0),
aes(group = -1),
fun.data = mean_se,
geom = "line", size = 3, colour = "green"
) +
geom_line(
data = ~ subset(., RML_ever_state == 1),
aes(colour = statename)
) + scale_x_discrete( limits=c(2008,2009,2010,2011,2012,2013,2014,2015,2016,2017))
state
, one thought is to view the summary of non-intervention states as a separate state (perhaps at this point call the column something different), then you will have all 50 states + the non-intervention summary; this would include it in the same legend. – stevebvars1
usingdput(vars1)
into the post. People will be able to reproduce by just cutting and pasting. You are likely to get answers in a few minutes vs. not at all or hours / days. However, the link I provided above may answer your question. – steveb