R 如何在geom_平滑和geom_点之间为同一组创建不同的颜色?

R 如何在geom_平滑和geom_点之间为同一组创建不同的颜色?,r,ggplot2,R,Ggplot2,我想要同一组的颜色。选择线型也很好 我希望给定线型的同一组geom_点的颜色与使用geom_点绘制的点略有不同。我希望给定组的线与点不同。我该怎么做呢 我已经创建了一些示例数据 注意:尝试在geom_smooth()中使用线型时出错 我不确定这是不是一个特别好的主意,因为你失去了一个关于什么点连接到什么回归线的视觉线索,但是下面的内容对我来说确实有用。基本上,我从ggplot()调用中提取了颜色美学,并将其分别传递给geom_point()和geom_smooth() library(ggplo

我想要同一组的颜色。选择线型也很好


我希望给定线型的同一组geom_点的颜色与使用geom_点绘制的点略有不同。我希望给定组的线与点不同。我该怎么做呢

我已经创建了一些示例数据

注意:尝试在geom_smooth()中使用线型时出错


我不确定这是不是一个特别好的主意,因为你失去了一个关于什么点连接到什么回归线的视觉线索,但是下面的内容对我来说确实有用。基本上,我从
ggplot()
调用中提取了颜色美学,并将其分别传递给
geom_point()
geom_smooth()

library(ggplot2)
library(scales)
#test data
obs=rep(1:3, each=30)
length(obs)
set.seed(50)
x=sample(seq(from = 20, to = 50, by = 5), size = 90, replace = TRUE)
y=sample(seq(from = 200, to = 500, by = 5), size = 90, replace = TRUE)

df = data.frame(obs,x,y)
ggplot(df, aes(x, y))+
geom_point(color = factor(obs))+
theme(legend.position="bottom")+
scale_x_continuous(breaks = seq(0, 50, by = 4),expand = c(0, 0), labels = comma_format())+
scale_y_continuous(breaks = seq(0, 500, by = 10),limits = c(0, 500),expand = c(0, 0), labels = comma_format())+
geom_smooth(aes(group=obs, color = factor(obs)), method="lm")+
scale_colour_manual(values = c("orange", "yellow","blue"),name = "Average Density Band:")
我改变了你的线条颜色,因为我的视力看不见它们。
我怀疑这不是为了这样工作,坦率地说,我不是100%确定为什么会这样。

不清楚您想要实现什么。我希望给定组的线与点不同。您可以使用
geom_线(aes(x,y,线型=因子(obs)),data=df)
为给定组添加线,并可以使用
缩放\u线型\u手册
更改线型。我不明白那是怎么回事。它需要一个软件包吗?是的,因为comma_format()在软件包库(scales)中,我相信我可能无法让R创建一个我想要的图表。我试图绘制一条回归线,其颜色与它所基于的数据点组不同。当我使用scale\u color\u manual()时,它也应用于基于给定组的所有点/线。i、 e.如果我将第一组颜色设置为蓝色,则回归线和基于第一组的点均为蓝色。我想让线显示黑色,点显示蓝色。
library(ggplot2)
library(scales)
#test data
obs=rep(1:3, each=30)
length(obs)
set.seed(50)
x=sample(seq(from = 20, to = 50, by = 5), size = 90, replace = TRUE)
y=sample(seq(from = 200, to = 500, by = 5), size = 90, replace = TRUE)

df = data.frame(obs,x,y)
ggplot(df, aes(x, y))+
geom_point(color = factor(obs))+
theme(legend.position="bottom")+
scale_x_continuous(breaks = seq(0, 50, by = 4),expand = c(0, 0), labels = comma_format())+
scale_y_continuous(breaks = seq(0, 500, by = 10),limits = c(0, 500),expand = c(0, 0), labels = comma_format())+
geom_smooth(aes(group=obs, color = factor(obs)), method="lm")+
scale_colour_manual(values = c("orange", "yellow","blue"),name = "Average Density Band:")