ggplot2;当颜色由变量编码时的单一回归线?
ggplot2; single regression line when colour is coded for by a variable?
我正在尝试使用一条回归线在 ggplot2
中创建一个散点图,即使颜色取决于 'Survey Type' 变量。理想情况下,我还想指定哪种调查类型是哪种颜色(社区 = 红色,地方 = 绿色,国家 = 蓝色)。
这是我 运行 的代码,它目前为我提供了 3 条独立的回归线,一条用于每种调查类型。
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 函数,它也不起作用,因为它会给我一个错误,提示 community is not a valid color name?
我的实际数据文件很大,所以我在这里只给出一个小例子:
data.male 数据集:
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
,您可以在色标规范中执行此操作(示例如下)。
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 ...
- 出于对色盲人士的礼貌,我建议 不要 使用主要 red/green/blue 作为您的颜色规范(尝试
scale_colour_brewer(palette="Dark1")
)。
我正在尝试使用一条回归线在 ggplot2
中创建一个散点图,即使颜色取决于 'Survey Type' 变量。理想情况下,我还想指定哪种调查类型是哪种颜色(社区 = 红色,地方 = 绿色,国家 = 蓝色)。
这是我 运行 的代码,它目前为我提供了 3 条独立的回归线,一条用于每种调查类型。
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 函数,它也不起作用,因为它会给我一个错误,提示 community is not a valid color name?
我的实际数据文件很大,所以我在这里只给出一个小例子:
data.male 数据集:
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
,您可以在色标规范中执行此操作(示例如下)。
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 ...
- 出于对色盲人士的礼貌,我建议 不要 使用主要 red/green/blue 作为您的颜色规范(尝试
scale_colour_brewer(palette="Dark1")
)。