GGPlot 不会显示超过 3 种颜色

GGPlot won't display more than 3 colors

GGPlot 仅显示前三种颜色(绿色、黄色和橙色),代码如下:

p = ggplot(MobileOutput, aes(x=`Timestamp(UTC)`,y=`PM2.5(ug/m3)`))+
  geom_point(aes(colour = cut(`PM2.5(ug/m3)`, c(0, 12.0, 35.4, 55.4, 150.4, 250.4, 500, Inf))),
             size = 0.1) +
  ylim(0,500)  +
  theme_bw() +
  scale_color_manual(name = "PM2.5",
                     values = c("(0,12]" = "green2",
                                "(12,35.4]" = "yellow2",
                                "(35.4,55.4]" = "orange",
                                "(55.4,150.4]" = "red1",
                                "(150.4, 250.4]" = "red2",
                                "(250.4, 500]" = "red3",
                                "(500, Inf]" = "red4"))
gPlotly <- ggplotly()

所有红色仍以不可见的 clear/white 颜色绘制。我能够将鼠标悬停在不可见数据上并查看其上的信息,确认它正在被绘制(见下图)。此外,绿色、黄色和橙色的点出现在图例中它们的范围旁边,而图例中有 none 个预期的红点。

如果我调整上面的代码以包括如下 3 个范围,所有颜色都会按预期显示:

p = ggplot(MobileOutput, aes(x=`Timestamp(UTC)`,y=`PM2.5(ug/m3)`))+
  geom_point(aes(colour = cut(`PM2.5(ug/m3)`, c(0, 12.0, 35.4, Inf))),
             size = 0.1) +
  ylim(0,500)  +
  theme_bw() +
  scale_color_manual(name = "PM2.5",
                     values = c("(0,12]" = "green2",
                                "(12,35.4]" = "yellow2",
                                "(35.4,Inf]" = "red4"))
gPlotly <- ggplotly()

如果我添加一个额外的范围,超出列出的第 3 个项目的任何内容都会再次完全不可见,如第一张图片所示。

有什么我可以调整的,让 GGPlot 支持在图例和绘图中显示超过 3 个 colors/ranges?

它的标签中的值看起来像 cut() 舍入:在此示例中,断点都位于 (x+0.4),但较大 bin 的端点在标签中以整数形式给出 (150, 250 而不是 150.4、250.4)。这将导致您的剪切 PM2.5 向量中的唯一 values/levels 与您在比例中指定的向量不匹配。

 table(cut(rnorm(1000,150,200), 
    breaks=c(0, 12.0, 35.4, 55.4, 150.4, 250.4, 500, Inf)))

 (0,12]   (12,35.4] (35.4,55.4]  (55.4,150]   (150,250]   (250,500] 
     17          31          25         194         199         282 
 (500,Inf] 
     43