首页 > 解决方案 > 点大小表示值时如何更改点大小的范围

问题描述

我想保留 ggplot2 散点图中点之间的相对大小差异,但增加所有点的大小,使它们更明显。

这是代码。唯一相关的一点是点大小与变量相关,所以我不想手动调整它。

ggplot(WSC5, aes(x = DCount, y = meandays, color = Department, size = NumCats)) + 
  geom_point() + 
  theme_minimal() +
  labs(y = "Mean Number of Days Open Per Case", 
       x = "Number of Cases", 
       title = "Cases", 
       size = "Number of Categories") +
  theme(plot.title = element_text(hjust = .5)) + 
  guides(color = guide_legend(override.aes = list(size =5)))

上面的代码生成了一个可行的图表,但这些点太小而无法在幻灯片演示中看到。我想要更大的分数。

标签: rggplot2scatter-plot

解决方案


欢迎来到stackoverflow。我想你可以得到你需要的scale_size()

df <-
  diamonds %>% 
  filter(clarity == "I1") %>%
  mutate(
    depth = floor(depth),
    price = round(price, -3)
  ) %>% 
  count(depth, price)

no_scale <-
  ggplot(df, aes(depth, price, size = n)) +
  geom_point(alpha = 0.3) +
  coord_fixed(ratio = 1/1000)

with_scale <-
  ggplot(df, aes(depth, price, size = n)) +
  geom_point(alpha = 0.3) +
  scale_size(
    breaks = c(1, 10, 25, 50),
    range = c(2, 6)
  ) +
  coord_fixed(ratio = 1/1000)             

gridExtra::grid.arrange(no_scale, with_scale, nrow = 1)

在此处输入图像描述

为了在这个网站上获得好的结果,总是提供一些代码来创建一个小数据集,这样我们就可以自己运行它。本文提供了有关如何执行此操作的详细信息:

https://reprex.tidyverse.org/articles/articles/datapasta-reprex.html


推荐阅读