在图例中包含年份(ggplot)但算作连续年份吗?

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

我正在尝试将年份作为点的颜色,但出于某种原因,它在图例中显示了带有 .0 和 .5 的年份,并且不确定如何修复。我认为它试图将我的岁月视为连续的(?)但不确定。

剧情截图如下:https://i.imgur.com/8bo06gq.png

代码:

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"
      )

同样,当我尝试制作 color = site (caterogy) 时,出现以下错误:

Error: Discrete value supplied to continuous scale

显然,我们没有您的数据,但我们可以使用玩具数据集复制您的问题:

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

最简单的方法是为色标设置分界点,确保所有分界点都是整数值:

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

reprex package (v2.0.1)

于 2022-05-31 创建