0
votes

I'm trying to generate a grouped geom_line plot with two grouped geom_hline overlaid. The groupings are the same in all 3 cases. The issue is that the legend only appears for geom_line (+ geom_point), but not for the two geom_hline commands. The ideal result would be 3 legends, each with 3 lines representing each group (with relevant dashed/dotted lines for geom_hline). Alternatively additional legends showing a black dashed and dotted line labelled as I have in the color variable for the geom_hline commands would also work. Data, code and plot as it stands below, thanks in advance for any assistance!

Data (dput):

acc.df <- structure(list(intersect.nn = structure(c(1L, 2L, 3L, 1L, 2L, 
3L, 1L, 2L, 3L), .Label = c("CD4", "CD8", "Treg"), class = "factor"), 
    prop = c(0.689912280701754, 0.999746643020015, 0.779661016949153, 
    0.671249111163783, 0.988757981832899, 0.769230769230769, 
    0.666112680737909, 0.981778644271146, 0.776223776223776), 
    K = c("k 1", "k 1", "k 1", "k 2", "k 2", "k 2", "k 3", "k 3", 
    "k 3")), row.names = c(NA, -9L), class = "data.frame")
acc.exp <- structure(list(intersect.exp = structure(1:3, .Label = c("CD4", 
"CD8", "Treg"), class = "factor"), prop = c(0.689912280701754, 
0.999746610921069, 0.779661016949153)), row.names = c(1L, 5L, 
9L), class = "data.frame")
acc.clust <- structure(list(seurat_clusters = structure(1:3, .Label = c("CD4", 
"CD8", "Treg"), class = "factor"), prop = c(0.666275954454119, 
0.981845461365341, 0.774647887323944)), row.names = c(1L, 5L, 
9L), class = "data.frame")

Code:

ggplot(acc.df, aes(x = K, y = prop, group = intersect.nn)) +
  geom_line(aes(color = intersect.nn)) +
  geom_point(aes(color = intersect.nn)) +
  geom_hline(data = acc.exp, aes(yintercept = prop, color = intersect.exp), show.legend = T, linetype = "dashed") +
  geom_hline(data = acc.clust, aes(yintercept = prop, color = seurat_clusters), show.legend = T, linetype = "dotted")

Plot as it stands

1

1 Answers

0
votes

Managed to work this out myself. Very quickly for someone in the same situation: don't use a dataframe for each geom_hline. I simply merged the dataframes (acc.df and acc.exp) together (adding a column variable to distinguish between them), then in a single geom_hline command setting the "color" and "linetype" aesthetics to my groupings.