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

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

Increase size of geom_point for one factor level

问题

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

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

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

ggplot(mtcars, aes(x = qsec , y = mpg, col = as.factor(cyl), size = as.factor(cyl))) +
  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?

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

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

I do not want to use the size option here:

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

because it uses size for all factor levels.

答案1

得分: 3

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

library(ggplot2)

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

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

英文:

Another way is to use ifelse to assign size.

library(ggplot2)

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

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

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

答案2

得分: 2

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

library(ggplot2)
library(dplyr)
mtcars %>%
  mutate(size = case_when(cyl == 6 ~ 2,
                          TRUE ~ 1)) %>%
  ggplot(aes(x = qsec, y = mpg, col = as.factor(cyl), size = size)) +
  geom_point() +
  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:

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

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

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

答案3

得分: 1

你可以使用两个数据集:

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

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

英文:

You can use two data sets:

ggplot() +
  aes(x = qsec, y = mpg, col = as.factor(cyl)) +
  geom_point(data = mtcars[mtcars$cyl != 6, ], size = 2) +
  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:

确定