增加一个因子水平的geom_point大小

huangapple go评论105阅读模式
英文:

Increase size of geom_point for one factor level

问题

想要增加仅在因子变量 cyl == 6 的数据点的 size。如何实现?

  1. ggplot(mtcars, aes(x = qsec , y = mpg, col = as.factor(cyl))) +
  2. geom_point(size=2, data = subset(mtcars, cyl == 6))

我不想在这里使用 size 选项:

  1. ggplot(mtcars, aes(x = qsec , y = mpg, col = as.factor(cyl), size = as.factor(cyl))) +
  2. geom_point()

因为它对所有因子水平使用了 size

英文:

Say I like to increase the size of only those data points where the factor variable cyl == 6. How can I do that?

  1. ggplot(mtcars, aes(x = qsec , y = mpg, col = as.factor(cyl ))) +
  2. geom_point(size= 2)

增加一个因子水平的geom_point大小

I do not want to use the size option here:

  1. ggplot(mtcars, aes(x = qsec , y = mpg, col = as.factor(cyl), size = as.factor(cyl))) +
  2. geom_point()

because it uses size for all factor levels.

答案1

得分: 3

另一种方法是使用 ifelse 分配 size

  1. library(ggplot2)
  2. ggplot(mtcars, aes(x = qsec , y = mpg, col = as.factor(cyl), size = ifelse(cyl == 6, 4, 3))) +
  3. geom_point() +
  4. guides(size = 'none')

增加一个因子水平的geom_point大小

英文:

Another way is to use ifelse to assign size.

  1. library(ggplot2)
  2. ggplot(mtcars, aes(x = qsec , y = mpg, col = as.factor(cyl), size = ifelse(cyl == 6, 4, 3))) +
  3. geom_point() +
  4. guides(size = 'none')

增加一个因子水平的geom_point大小

<sup>Created on 2023-03-09 with reprex v2.0.2</sup>

答案2

得分: 2

你可以创建一个带有每个因素的大小的列,并在aes中使用大小,像这样:

  1. library(ggplot2)
  2. library(dplyr)
  3. mtcars %>%
  4. mutate(size = case_when(cyl == 6 ~ 2,
  5. TRUE ~ 1)) %>%
  6. ggplot(aes(x = qsec, y = mpg, col = as.factor(cyl), size = size)) +
  7. geom_point() +
  8. guides(size = 'none')

增加一个因子水平的geom_point大小

使用 reprex v2.0.2 创建于2023-03-09

英文:

You could create a column with the size for each factor and use size in aes like this:

  1. library(ggplot2)
  2. library(dplyr)
  3. mtcars %&gt;%
  4. mutate(size = case_when(cyl == 6 ~ 2,
  5. TRUE ~1)) %&gt;%
  6. ggplot(aes(x = qsec , y = mpg, col = as.factor(cyl), size = size)) +
  7. geom_point() +
  8. guides(size = &#39;none&#39;)

增加一个因子水平的geom_point大小<!-- -->

<sup>Created on 2023-03-09 with reprex v2.0.2</sup>

答案3

得分: 1

你可以使用两个数据集:

  1. ggplot() +
  2. aes(x = qsec, y = mpg, col = as.factor(cyl)) +
  3. geom_point(data = mtcars[mtcars$cyl != 6, ], size = 2) +
  4. geom_point(data = mtcars[mtcars$cyl == 6, ], size = 5)

增加一个因子水平的geom_point大小

英文:

You can use two data sets:

  1. ggplot() +
  2. aes(x = qsec, y = mpg, col = as.factor(cyl)) +
  3. geom_point(data = mtcars[mtcars$cyl != 6, ], size = 2) +
  4. geom_point(data = mtcars[mtcars$cyl == 6, ], size = 5)

增加一个因子水平的geom_point大小

huangapple
  • 本文由 发表于 2023年3月9日 23:36:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686810.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定