ggplot2;变量编码颜色时的一条回归线? [英] ggplot2; single regression line when colour is coded for by a variable?
问题描述
我试图在ggplot2
中用一条回归线创建散点图,即使颜色取决于调查类型"变量.理想情况下,我还要指定哪种调查类型是哪种颜色(社区=红色,国家以下=绿色,国家=蓝色).
I am trying to create a scatterplot in ggplot2
with one regression line even though colour is dependent on the 'Survey Type' variable. I would ideally also like to specify which survey type is which colour (community = red, subnational = green, national = blue).
这是我正在运行的代码,目前为我提供3条单独的回归线,每种调查类型对应一条.
This is the code I'm running which currently gives me 3 separate regression lines, one for each survey type.
ggplot(data=data.male,aes(x=mid_year, y=mean_tc, colour =condition)) +
geom_point(shape=1) +
geom_smooth(method=lm, data=data.male, na.rm = TRUE, fullrange= TRUE)
条件是:
condition <- (data.male$survey_type)
即使我将颜色美感移到了geom_point函数上,它也不起作用,因为它给我一个错误消息,指出社区不是有效的颜色名称?
Even if I move the colour aesthetic to the geom_point function it doesn't work as it gives me an error saying community is not a valid colour name?
我的实际数据文件非常大,因此我在这里仅提供一个小示例:
My actual data file is really big so I'll just give a small sample here:
data.male数据集:
data.male dataset:
mid_year mean_tc survey_type
2000 4 Community
2001 5 National
2002 5.1 Subnational
2003 4.3 National
2004 4.5 Community
2005 5.2 Subnational
2006 4.4 National
推荐答案
data.male <- read.table(header=TRUE,text="
mid_year mean_tc survey_type
2000 4 Community
2001 5 National
2002 5.1 Subnational
2003 4.3 National
2004 4.5 Community
2005 5.2 Subnational
2006 4.4 National")
- 在
geom_smooth()
规范中使用aes(group=1)
来忽略通过将颜色映射分配给调查类型而引起的按调查类型分组. (或者,您可以将颜色映射放在geom_point()
中,而不是整个ggplot()
规范中.) - 如果要指定颜色,则需要在数据框中指定颜色作为变量的名称(即
survey_type
);如果要将图例中的名称更改为condition
,则可以在色标规范中进行更改(以下示例). - Use
aes(group=1)
in thegeom_smooth()
specification to ignore the grouping by survey type induced by assigning the colour mapping to survey type. (Alternatively, you can put the colour mapping intogeom_point()
rather than the overallggplot()
specification.) - If you want to specify colour you need to give it as the name of a variable in your data frame (i.e.,
survey_type
); if you want to change the name in the legend tocondition
you can do that in the colour scale specification (example below).
library(ggplot2); theme_set(theme_bw())
ggplot(data=data.male,aes(x=mid_year, y=mean_tc, colour=survey_type)) +
geom_point(shape=1) +
## use aes(group=1) for single regression line across groups;
## don't need to re-specify data argument
## set colour to black (from default blue) to avoid confusion
## with national (blue) points
geom_smooth(method=lm, na.rm = TRUE, fullrange= TRUE,
aes(group=1),colour="black")+
scale_colour_manual(name="condition",
values=c("red","blue","green"))
## in factor level order; probably better to
## specify 'breaks' explicitly ...
- 出于对色盲人群的礼貌,我建议不使用红色/绿色/蓝色为主色(尝试使用
scale_colour_brewer(palette="Dark1")
). - Out of courtesy to colour-blind people I would suggest not using primary red/green/blue as your colour specifications (try
scale_colour_brewer(palette="Dark1")
instead).
这篇关于ggplot2;变量编码颜色时的一条回归线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!