繁体   English   中英

ggplot2 中离散比例的连续图例

[英]Continuous legend to discrete scale in ggplot2

我正在尝试使用 viridis 调色板绘制连续数据(或转换为具有 104 个级别的因子,代表从 1 到 800 的值)。 如果我让数据连续,一些较低级别的方面无法通过颜色区分。 但是图例看起来应该是(连续)。 如果我离散数据并绘制它,颜色更容易区分,但我不能使用图例,因为它有 104 个 bin。 此外,如果我使用具有连续尺度的离散化数据,我会得到正确的结果,但图例显示的限制为 104,而不是值(从 1 到 800 的各种值)。 我怎样才能用易于区分的颜色绘制因子水平,但连续的尺度显示值上升到 800?

我尝试了 new_scale_color,但这也不起作用。

 # first layer: all countries, no fill, no white outlines
  geom_polygon(data = maps2, 
               aes(x = long, y = lat, group = group), fill="grey", show.legend = F, size = 0.1) +

# ​this gives me the right legend, but the lower factor levels are not distingishable in the graph, they all have the same color. 

  geom_polygon(data = test,
            aes(x = long, y = lat, group = group, fill = 
 citation_tally), show.legend = F)+ 

 # This, combined with as.factor(citation_tally) gives me distinguishable colors but a huge legend. 

 # scale_fill_viridis_d("discrete",option="D", na.value = NA)+

 # this changes the colors and makes them again indistinguishable
 
scale_fill_binned(type= "viridis")+ 

  theme(plot.title = element_text(hjust = 0.5, size = 6),
        axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks = element_blank(),
        axis.title.y=element_blank(),
        axis.title.x=element_blank(),
        rect = element_blank())+
  coord_fixed(1.3)+
  theme(plot.margin=unit(c(0.5,0.5,0.5,0.5), "cm"))


[![Continuous scale, legend is nice - colors indistinguishable][1]][1]


[![Discrete scale, colors are nice, but the legend is huge][2]][2]


  [1]: https://i.stack.imgur.com/6vJLA.png
  [2]: https://i.stack.imgur.com/QuN7C.png

您的数据不是分类数据,因此您不应使用离散比例,也不应使用factor() 其症状之一是您提到的巨大传说。

尝试绘制连续数据,然后使用plot + scale_fill_binned()这会给你一个分箱比例(这似乎是你想要的),但不是离散比例。 有关更多信息,请参阅有关此功能文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM