Adjust geom_point size so large values are plotted, but do not appear larger in ggplot2?

老子叫甜甜 提交于 2021-02-07 19:42:00

问题


The issue I am having is that I would like the points above a certain threshold (=<25) to not produce points larger than the set scale. These larger points still need to be displayed, and cannot be excluded:

d=data.frame(y=c(1,2,6,4,4,6,7,8),
             x=c(8,4,7,5,4,9,2,3),
             coverage=c(0,6,9,88,25,22,17,100),
             col=c(0,.25,.50,.76,.80,1.00,.11,.34)
             )
ggplot() + 
  scale_size(range = c(0, 13),
             breaks = c(0, 5, 10, 20, 25),
             labels = c("0", "5", "10", "20", "25+"),
             guide = "legend"
  ) +
  geom_point(data = d, mapping = aes(x = x, y = y, color = col, size = coverage)) +
  labs(title = "geom_point")

In the above sample code I have two points which have a "coverage" larger than 25+ and are outside of the scale. I would like these points to appear the same size as the 25+ threshold.


回答1:


I think this is what you're looking for:

d %>%
  mutate(coverage_trunc = pmin(coverage, 25)) %>%
ggplot() + 
  geom_point(mapping=aes(x=x, y=y, color=col, size=coverage_trunc)) +
  labs(title="geom_point") + 
  scale_size(range=c(0,13),
             breaks=c(0,5,10,20,25),
             labels=c("0","5","10","20","25+"),
             name = "Coverage Truncated",
             guide="legend")




回答2:


A ggplot only solution is to set the scale limits, and define the out of bounds behavior (oob) correctly. For size, one can use this scale:

ggplot(d, aes(x, y, color = col, size = coverage)) + 
  geom_point() +
  scale_size_area(
    max_size = 13,
    breaks = c(0,5,10,20,25),
    labels = c("0","5","10","20","25+"),
    guide = "legend",
    limits = c(0, 25),
    oob = scales::squish
  )



来源:https://stackoverflow.com/questions/54013902/adjust-geom-point-size-so-large-values-are-plotted-but-do-not-appear-larger-in

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!