gpt4 book ai didi

r - 如何在R中的不同方面产生不同的geom_vline?

转载 作者:行者123 更新时间:2023-12-02 04:13:16 24 4
gpt4 key购买 nike

我正在尝试在数据集的 2 个不同方面生成 2 条具有不同颜色的不同 geom_vlines。我这样做是为了突出两个不同方面的含义。

这是数据集:

Pclass  Sex    Age  SibSp   Parch   Fare    Cabin   Embarked    Survived
3 male 22 1 0 7.25 S 0
1 female 38 1 0 71.2833 C85 C 1
3 female 26 0 0 7.925 S 1
1 female 35 1 0 53.1 C123 S 1
3 male 35 0 0 8.05 S 0
1 male 54 0 0 51.8625 E46 S 0

代码如下:

g<-ggplot(data = train3, aes(x = Age, y = Survived, colour = factor(Pclass)))
g<-g+facet_wrap(~Sex)
g<-g+geom_point(size = 4, alpha = 0.2)+ggtitle("Survival by Gender")+theme(plot.title = element_text(hjust = 0.5))
g<-g+geom_vline(data = subset(train3,Sex=="female"), xintercept = mean(train3[which(train3$Sex=="female"),3]), colour = "pink", size = 1)
g<-g+geom_vline(data = subset(train3,Sex=="male"), xintercept = mean(train3[which(train3$Sex=="male"),3]), colour = "blue", size = 1)
g

这是输出

enter image description here

实际上,我只想在每个方面产生 1 个 vline:雌性为粉红色,雄性为蓝色。

给出的建议here也不工作。显示错误为:

Error in .(Sex == "female") : could not find function "."

最佳答案

以下是如何为不同的鸢尾品种添加不同的geom_vline:

ggplot(iris, aes(Sepal.Length, Petal.Length)) + facet_wrap(~Species, scales="free") + geom_point() + 
geom_vline(data=filter(iris, Species=="setosa"), aes(xintercept=5), colour="pink") +
geom_vline(data=filter(iris, Species=="versicolor"), aes(xintercept=6), colour="blue") +
geom_hline(data=filter(iris, Species=="virginica"), aes(yintercept=6), colour="green")

enter image description here

关于r - 如何在R中的不同方面产生不同的geom_vline?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44196384/

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