gpt4 book ai didi

r - 使用条件填充美学向 ggplot 添加额外的方面

转载 作者:行者123 更新时间:2023-12-02 01:27:40 25 4
gpt4 key购买 nike

以下问题:Merge ggplot fill and size legend keys

我有这些数据,它产生以下图:

library(tidyverse)
dframe <- as.data.frame(Titanic)
dframe$First<-ifelse(dframe$Class=="1st",1,0)

dframe %>%
ggplot(aes(Class, Sex))+
geom_point(aes(size = Freq, fill = Freq), pch = 21)+
guides(fill = guide_legend(reverse = TRUE, override.aes = list(alpha = 1)))+
guides(size = guide_legend(reverse = TRUE))+
facet_grid(Survived~Age)+
theme_bw()+
labs(x = "Survival Rate",
y = "Gender")

enter image description here

现在,假设我想调整某些变量的填充 - 例如,所有第一个子项:

dframe$First<-ifelse(dframe$Class=="1st",1,0)

enter image description here

有什么想法吗?

最佳答案

您可以在顶部放置一组新的点。只需过滤您要添加的数据即可。

library(tidyverse)


dframe <- as.data.frame(Titanic)
dframe$First<-ifelse(dframe$Class=="1st",1,0)

dframe %>%
ggplot(aes(Class, Sex))+
geom_point(aes(size = Freq, fill = Freq), pch = 21)+
geom_point(data = . %>% filter(Age == "Child" & Class == "1st"),
aes(color = "1st Class Children"),
pch = 22,
size = 2,
fill = "red")+
guides(fill = guide_legend(reverse = TRUE, override.aes = list(alpha = 1)))+
guides(size = guide_legend(reverse = TRUE))+
facet_grid(Survived~Age)+
theme_bw()+
labs(x = "Survival Rate",
y = "Gender",
color = "")

关于r - 使用条件填充美学向 ggplot 添加额外的方面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74087233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com