0

Im making a scatterplot which shows a value plotted against the date since symptom onset. These patients are categorised based on disease severity, and i wanted to show how the values change over time in each severity category. I have coloured the dots based on severity score, but i prefer to use shape =21 so i can have a border. I also draw a line to see the trend, and i want that coloured in the same way, however, this has added another legend and it looks complicated. This issue doesnt happen if use a different shape that isnt filled, because scale_colour_manual can be used for both the lines and the dots, but i dont think it looks as nice. Any idea how i can fix this?

IC50SymObySS <- ggplot(data = isaric) +
  geom_point(mapping = aes(x = Days_since_onset, y = log2IC50, fill = Severity_score),  size = 2, colour = "black", shape = 21)+
  geom_smooth(mapping = aes(x = Days_since_onset, y = log2IC50, colour = Severity_score), se = FALSE)+
  scale_fill_manual(breaks=c("1","2","3","4","5"),
                      values=c("1" = "lightblue1","2" = "lightblue3","3" = "lightblue4","4" = "lightcoral","5" = "firebrick2"),
                      labels=c("1","2","3","4","5"),
                      name = "Severity Score")+
  scale_colour_manual(values=c("1" = "lightblue1","2" = "lightblue3","3" = "lightblue4","4" = "lightcoral","5" = "firebrick2"))+
  theme_minimal()+
  JTheme+
  ylab("Serum Log2 IC50")+
  xlab("Days Since Symptom Onset")+
  guides(colour = guide_legend(title.position = "top", title.hjust = 0.5))
IC50SymObySS 

plot with bad legend

lordjord93
  • 47
  • 1
  • 7

1 Answers1

1

As per this answer, you need to use identical name and labels values for both fill and colour scale.

library(ggplot2)
library(dplyr)
isaric <- transmute(iris, 
    Days_since_onset = (Sepal.Length - 4)^3, 
    log2IC50 = Sepal.Width * 3, 
    Severity_score = cut(Petal.Length, breaks = quantile(Petal.Length, prob = 0:5 / 5), labels = 1:5))

ggplot(data = isaric) +
    geom_smooth(mapping = aes(x = Days_since_onset, y = log2IC50, colour = Severity_score), se = FALSE)+
    geom_point(mapping = aes(x = Days_since_onset, y = log2IC50, fill = Severity_score),  size = 2, colour = "black", shape = 21)+
    scale_colour_manual(
        name = "Severity Score", 
        values=c("1" = "lightblue1","2" = "lightblue3","3" = "lightblue4","4" = "lightcoral","5" = "firebrick2"), 
        labels=c("1","2","3","4","5"))+
    scale_fill_manual(
        name = "Severity Score", 
        breaks=c("1","2","3","4","5"),
        values=c("1" = "lightblue1","2" = "lightblue3","3" = "lightblue4","4" = "lightcoral","5" = "firebrick2"),
        labels=c("1","2","3","4","5"))+
    theme_minimal()+
    ylab("Serum Log2 IC50")+
    xlab("Days Since Symptom Onset")+
    guides(colour = guide_legend(title.position = "top", title.hjust = 0.5))

Scatterplot with combined legend

CSJCampbell
  • 2,025
  • 15
  • 19