Home > Mobile >  Include year in legend (ggplot) but counts as continuous?
Include year in legend (ggplot) but counts as continuous?

Time:06-04

I am trying to include year as a colour for the points, but for some reason, it shows year with .0 and .5 in the legend, and unsure how to fix. I think it's trying to treat my years as continuous (?) but unsure.

Here's a picture of the plot: https://i.imgur.com/8bo06gq.png

And the code:

ggplot(data = weather1, aes(x = avgt30, y = dsi, colour = year(date)))  
      theme_bw()  
      geom_count()  
      scale_colour_viridis_c( end = 0.8)  
      scale_y_continuous(trans = "log1p")   
      stat_smooth(method = "lm", formula = y ~ x   I(x^2), colour = "orange")   
      labs(
        x = "Temperature (\u00B0C)",
        y = "Disease Severity Index",
        title = "DSI vs. Mean 30-day Temp"
      )

Similarly, when I try to make the colour = site (caterogy), I get the following error:

Error: Discrete value supplied to continuous scale

CodePudding user response:

Obviously, we don't have your data, but we can replicate your problem with a toy data set:

library(ggplot2)

df <- data.frame(year = 2011:2020, x = 1:10, y = sin(1:10))

p <- ggplot(df, aes(x, y, color = year))   
  geom_point()

p

The easiest way round this is to set the breaks for the color scale, ensuring that all the breaks are integer values:

p   scale_color_continuous(breaks = seq(2011, 2020, 2))

Created on 2022-05-31 by the reprex package (v2.0.1)

  • Related