英文:
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)
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')
英文:
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')
<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')
使用 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 %>%
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')
<!-- -->
<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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论