代码之家  ›  专栏  ›  技术社区  ›  Miksmith

ggplot2从离散图例中删除项

  •  1
  • Miksmith  · 技术社区  · 6 年前

    使用ColorBrewer离散颜色,我的ggplot2图例不会显示使用geom_线绘制的所有级别。在总结了StackExchange上的类似问题之后,我尝试在两台天平的蒸馏器上添加drop=false,但这并不能解决问题。

    我在这里做错了什么?下面是可重复的代码

    谢谢

    迈克

    library(tidyverse)
    
    Year <- c(2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2019)
    Month <- c(4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1)
    Cum_Total <- c(0,0,0,0,0,0,0,0,0,0,0, 14558, 14568, 14674, 14757, 14807, 14902, 14950, 15024, 15049, 15203, 14775, 14906, 14905, 14991, 15004, 15055, 15112, 15170, 15166, 15273, 15417, 15581, 14939, 15028, 15038, 15127, 15222, 15286, 15395, 15418, 15476, 15528, 15507, 15589, 14919, 15021, 15056, 15115, 15137, 15176, 15176, 15248, 15229, 15277, 15269, 15402, 14839, 14849, 14912, 14929, 14913, 14948, 15015, 15022, 15096, 15046, 15089, 15115, 14737, 14700, 14733, 14694, 14767, 14804, 14730, 14752, 14943, 14918, 14937, 14664, 13081)
    Cum_Total[Cum_Total == 0] <- NA
    results <- data.frame(Year,Month,Cum_Total)
    
    ggplot(results,aes(x=Month,y=Cum_Total, group=Year, color=Year)) +
      geom_line() +
      geom_point() +
      scale_colour_distiller(palette = "Dark2", direction=-1, guide="legend") +
      scale_y_continuous(name="Cumulative Total", limits=c(13000,16000)) +
      scale_x_continuous(name="Month", limits=c(1, 12), breaks=c(2,4,6,8,10,12))
    
    1 回复  |  直到 6 年前
        1
  •  3
  •   www    6 年前

    使用 breaks 指定要显示的所有年份。

    ggplot(results,aes(x=Month,y=Cum_Total, group=Year, color=Year)) +
      geom_line() +
      geom_point() +
      scale_colour_distiller(palette = "Dark2", breaks = unique(results$Year),
                             direction = -1, guide = "legend") +
      scale_y_continuous(name="Cumulative Total", limits=c(13000,16000)) +
      scale_x_continuous(name="Month", limits=c(1, 12), breaks=c(2,4,6,8,10,12))
    

    enter image description here

    或将年份改为 factor 使用 scale_colour_brewer .

    ggplot(results,aes(x=Month,y=Cum_Total, group=Year, color = factor(Year))) +
      geom_line() +
      geom_point() +
      scale_colour_brewer(palette = "Dark2", direction = -1, guide = "legend") +
      scale_y_continuous(name="Cumulative Total", limits=c(13000,16000)) +
      scale_x_continuous(name="Month", limits=c(1, 12), breaks=c(2,4,6,8,10,12)) 
    

    enter image description here