gpt4 book ai didi

r - 如何在R中的ggplot中将图例添加到geom_smooth

转载 作者:行者123 更新时间:2023-12-04 05:37:09 26 4
gpt4 key购买 nike

在ggplot中将图例添加到其他平滑图时遇到问题。

    library(splines)
library(ggplot2)
temp <- data.frame(x = rnorm(200, 20, 15), y = rnorm(200, 30, 8))

ggplot(data = temp, aes(x, y)) + geom_point() +
geom_smooth(method = 'lm', formula = y ~ bs(x, df=5, intercept = T), col='blue') +
geom_smooth(method = 'lm', formula = y ~ ns(x, df=2, intercept = T), col='red')


我有两个样条线:红色和蓝色。如何为他们添加图例?

最佳答案

将颜色放入aes()并添加scale_colour_manual()

ggplot(data = temp, aes(x, y)) + geom_point() + 
geom_smooth(method = 'lm', formula = y ~ bs(x, df=5, intercept = T), aes(colour="A")) +
geom_smooth(method = 'lm', formula = y ~ ns(x, df=2, intercept = T), aes(colour="B")) +
scale_colour_manual(name="legend", values=c("blue", "red"))


enter image description here

关于r - 如何在R中的ggplot中将图例添加到geom_smooth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36276240/

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