我试图ggplot2用一条回归线创建一个散点图,即使颜色取决于“调查类型”变量。理想情况下,我还想指定哪种调查类型是哪种颜色(社区 = 红色,国家以下地区 = 绿色,国家 = 蓝色)。
这是我正在运行的代码,它目前为我提供了 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)
Run Code Online (Sandbox Code Playgroud)
条件是:
condition <- (data.male$survey_type)
Run Code Online (Sandbox Code Playgroud)
即使我将颜色美学移到 geom_point 函数它也不起作用,因为它给我一个错误,说社区不是一个有效的颜色名称?
我的实际数据文件非常大,所以我在这里只给出一个小样本:
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
Run Code Online (Sandbox Code Playgroud) 我不知道如何得到回归线方程,我用函数geom_smooth绘制的线性回归的r ^ 2和p值.
这是我的代码:
g <- ggplot(data=data.male, aes(x=mid_year, y=mean_tc, colour=data.male$survey_type))
g <- g + geom_point(shape = 20, size =2)
g <- g + geom_smooth(method=lm, na.rm = FALSE, se = TRUE, aes(group=1), colour = "black")
g <- g + theme_gray(base_size=24)
g <- g+ xlab("Year")
g <- g + ylab("Mean serum total cholesterol (mmol/L)")
g <- g + theme(legend.position="bottom")
g <- g + scale_y_continuous(limits=c(3.5,6.5), breaks=c(3.5,4,4.5,5,5.5,6,6.5))
g <- g + scale_x_continuous(limits=c(1980,2015), breaks=c(1980,1990,2000,2010))
g <- g + scale_colour_manual(name = "Survey Type", values= c("Red", "Blue", …Run Code Online (Sandbox Code Playgroud)