首页 > 解决方案 > 调整 geom_point 大小以便绘制大值,但在 ggplot2 中不会显得更大?

问题描述

我遇到的问题是我希望高于某个阈值(=<25)的点不会产生大于设定比例的点。这些较大的点仍然需要显示,不能排除:

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

在上面的示例代码中,我有两个点的“覆盖率”大于 25+,并且在比例之外。我希望这些点的大小与 25+ 阈值相同。

示例绘图输出

标签: rggplot2plot

解决方案


我想这就是你要找的:

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

截断覆盖的图片


推荐阅读