调整geom_point的大小,以便绘制较大的值,但在ggplot2中看起来不大吗?

时间:2019-01-02 22:18:17

标签: r ggplot2 plot

我遇到的问题是,我希望高于某个阈值(= <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+阈值相同。

Example Plot output

2 个答案:

答案 0 :(得分:3)

我认为这是您想要的:

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

Picture of Truncated Coverage

答案 1 :(得分:0)

唯一的ggplot解决方案是设置比例尺限制,并正确定义越界行为(oob)。对于尺寸,可以使用此比例尺:

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
  )

enter image description here