绘制趋势线和ggplot中多个变量的方程式
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了绘制趋势线和ggplot中多个变量的方程式相关的知识,希望对你有一定的参考价值。
我正在尝试使用ggplot geom_smooth()
绘制三个变量(SA,SA1,SA2)的具有R平方的趋势线和方程。在绘制三个变量时,我只能得到一条趋势线。这种情节可能是什么原因和解决方案? DATA LINK(3条趋势线+ 3个等式,R ^ 2)
library(ggplot2)
test <- read.xlsx2("filepath/test.xlsx", 1, header=TRUE)
> test
year SA SA1 SA2
1 2008 1.409155e+15 3.632740e+17 4.06998e+15
2 2009 1.533598e+15 3.767342e+17 4.05015e+15
..
..
10 2017 1.761596e+15 3.581407e+17 3.03403e+15
11 2018 1.677707e+15 3.428239e+17 3.15862e+15
dput(test)
structure(list(year = structure(1:11, .Label = c("2008", "2009",
"2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017",
"2018"), class = "factor"), SA = c(1409155313839800, 1533598052716370,
1524727969175020, 1583941250825040, 1597021832828680, 1549362217661020,
1607700438214130, 1592107298305410, 1735331260744350, 1761596167580970,
1677707298223350), SA1 = c(363273957183114432, 376734225895083200,
355896023882281984, 368398075167704192, 367791249493954048, 360257619620708800,
360061958768956736, 367763926166363648, 355088403981918272, 358140732212706304,
342823915606135936), SA2 = c(4.06998e+15, 4.05015e+15, 3.94057e+15,
3.9507e+15, 3.58963e+15, 3.53037e+15, 3.43302e+15, 3.20139e+15,
3.94638e+15, 3.03403e+15, 3.15862e+15)), row.names = c(NA, -11L
), class = "data.frame")
test$SA=as.numeric(levels(test$SA))[test$SA]
test$SA1=as.numeric(levels(test$SA1))[test$SA1]
test$SA2=as.numeric(levels(test$SA2))[test$SA2]
ggplot(test,aes(x=year, y= SA, group = 1)) + geom_line(mapping = aes(x = test$year, y = test$SA)) +
geom_line(mapping = aes(x = test$year,y = test$SA2), color = "red")+ geom_line(mapping = aes(x = test$year, y = test$SA1/100), + geom_smooth(method = "lm")
size = 1, color = "blue")+ scale_y_continuous(name = " Primary axis", sec.axis = sec_axis(~.*100, name = "Secondary axis"))
我还尝试过重塑数据,然后绘制3个变量的趋势线,但副轴却被扭曲了。
df <- reshape2::melt(test, id.var = "year")
df
year variable value
1 2008 SA 1.409155e+15
2 2009 SA 1.533598e+15
3 2010 SA1 1.524728e+15
4 2011 SA1 1.583941e+15
..
..
5 2017 SA2 1.597022e+15
6 2018 SA2 1.549362e+15
ggplot(df,aes(x=year, y= value, group = variable)) + geom_line()+ scale_y_continuous(name = "y axis", sec.axis = sec_axis(~.*100, name = "y axis"))+ geom_smooth(method = "lm")
答案
这类问题通常是数据重组问题。参见reshaping data.frame from wide to long format。并且使用已发布的数据,无需将SA
,SA1
和SA2
列强制转换为数字,因此它们不会作为要素发布。
library(dplyr)
library(tidyr)
library(ggplot2)
test %>%
mutate(SA1 = SA1/100) %>%
gather(sa, value, -year) %>%
ggplot(aes(x = year, y = value, group = sa, colour = sa)) +
geom_line() +
geom_smooth(method = "lm", formula = y ~ x) +
scale_color_manual(values = c("black", "red", "blue")) +
scale_y_continuous(name = " Primary axis", sec.axis = sec_axis(~.*100, name = "Secondary axis"))
以上是关于绘制趋势线和ggplot中多个变量的方程式的主要内容,如果未能解决你的问题,请参考以下文章