回归返回2个以前标签_ggplot2:使用斜率和p值标签面对多个回归

解决办法:

首先导入&&子集化数据

#I called mine Ancovas. --> Note, export your df as .csv to work with it in R.

Ancovas

#Next, subset your data by the two conditions (e.g. "l"=light, "d"=dark), and both treatments (e.g. "MQ"=water, "DOM"=media)

AncovasL

AncovasL.MQ

AncovasL.DOM

AncovasD

AncovasD.MQ

AncovasD.DOM

创建回归函数

#--> this code was gathered from several sites.

#note: I don't understand the logic of how the numbers in brackets are organized. But this essentially pulls some information from the fit model. i.e. [9] means find the 9th value in the list (I think)

regression = function(Ancovas){

fit

slope

intercept

R2

R2.Adj

p.val

c(slope,intercept,R2,R2.Adj, p.val) }

现在通过TREATMENT拆分回归数据并应用回归函数

#Call your column "Treatments"

regressions_dataL.MQ

regressions_dataL.DOM

regressions_dataD.MQ

regressions_dataD.DOM

#Rename columns

colnames(regressions_dataL.MQ)

colnames(regressions_dataL.DOM)

colnames(regressions_dataD.MQ)

colnames(regressions_dataD.DOM)

为数字创建主题

#Yes I like to hyper control every aspect of my theme

theme_new

legend.key = element_rect(fill = "white"), panel.grid.minor = element_blank(), panel.grid.major = element_blank(),

axis.text.x=element_text(size = 11, angle = 0, hjust=0.5), #axis numbers (set it to 1 to place it on left side, 0.5 for middle and 0 for right side)

axis.text.y=element_text(size = 13, angle = 0),

plot.title=element_text(size=15, vjust=0, hjust=0), #hjust 0.5 to center title

axis.title.x=element_text(size=14), #X-axis title

axis.title.y=element_text(size=14, vjust=1.5), #Y-axis title

legend.position = "top",

legend.title = element_text(size = 11, colour = "black"), #Legend title

legend.text = element_text(size = 8, colour = "black", angle = 0), #Legend text

strip.text.x = element_text(size = 9, colour = "black", angle = 0), #Facet x text size

strip.text.y = element_text(size = 9, colour = "black", angle = 270)) #Facet y text size

guides_new

Colours

rainbow_hcl(length(levels(factor(StackedTable$DOM))), start = 30, end = 300) #Yes I am Canadian so Colours has a "u"

Colours[5]

Colours[4]

Colours[3]

Colours[2]

Colours[1]

创建两个稍后合并的数字

#Making plot for panel A ("Dark condition")

PlotA

ggplot(AncovasD, aes(x=as.numeric(Time.h), y=as.numeric(Measurement), fill=as.factor(Treatment))) +

geom_smooth(data=subset(AncovasD,Treatment =="MQ"), aes(Time.h,Measurement,color=factor(Treatment)),method="lm", formula = y~x, se=T, show.legend = F) +

geom_smooth(data=subset(AncovasD,Treatment =="DOM"), aes(Time.h,Measurement,color=factor(Treatment)),method="lm", formula = y~x, se=T, show.legend = F) + #You need this line twice, once for each condition

geom_errorbar(data=AncovasD, aes(ymin=Measurement-SD, ymax=Measurement+SD), width=0.2, colour="#73777a", size = 0.5) + #Change width based on the size of your X-axis

geom_point(shape = 21, size = 3, colour = "black", stroke = 1) + #colour is the outline of the circle, stroke is the thickness of that outline

facet_grid(Treatment ~ UV) + #This places all your treatments into a grid. Change the order if you want them horizontal. Use "." if you do not want a label.

geom_label(data=regressions_dataD.MQ, inherit.aes=FALSE, size=0.7, colour=Colours[1], #Add label for DOM regressions, specify same colour as your legend, change size depending on how large you want the text

aes(x=-0.1, y=41, label=paste(" ", "m == ", slope, "\n " , #replace this line with the values you want: e.g. R-squared=("R2 == ", R2.Adj) ; intercept=("b == ", intercept). The "\n " makes a second line

" ", "p == ", p.val ))) + #This completes the first label. Repeat same process for second label.

geom_label(data=regressions_dataD.DOM, inherit.aes=FALSE, size=0.7, colour=Colours[2],

aes(x=-0.1, y=4, label=paste(" ", "m == ", slope, "\n " ,

" ", "p == ", p.val )))

#Now for the irradiated samples "light" plot (Panel B)

PlotB

ggplot(AncovasL, aes(x=as.numeric(Time.h), y=as.numeric(Measurement), fill=as.factor(Treatment))) + #Same as above but use your second dataframe.

geom_smooth(data=subset(AncovasL,Treatment =="MQ"), aes(Time.h,Measurement,color=factor(Treatment)),method="lm", formula = y~x, se=T, show.legend = F) +

geom_smooth(data=subset(AncovasL,Treatment =="DOM"), aes(Time.h,Measurement,color=factor(Treatment)),method="lm", formula = y~x, se=T, show.legend = F) +

geom_errorbar(data=AncovasL, aes(ymin=Measurement-SD, ymax=Measurement+SD), width=0.2, colour="#73777a", size = 0.5) +

geom_point(shape = 21, size = 3, colour = "black", stroke = 1) +

facet_grid(Treatment ~ UV) +

geom_label(data=regressions_dataL.MQ, inherit.aes=FALSE, size=0.7, colour=Colours[1],

aes(x=-0.1, y=41, label=paste(" ", "m == ", slope, "\n " ,

" ", "p == ", p.val ))) +

geom_label(data=regressions_dataL.DOM, inherit.aes=FALSE, size=0.7, colour=Colours[2],

aes(x=-0.1, y=4, label=paste(" ", "m == ", slope, "\n " ,

" ", "p == ", p.val )))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值