简体   繁体   English

使用ggplot2在data.frame中的所有列上执行lm功能

[英]Using ggplot2 for lm function on allcolumns in data.frame

My data looks like as shown below: 我的数据如下图所示:

Fasting_glucose sample  Prevotella  Turicibacter    Mitsuokella Description
138 PCS119F 0.005782    0   0   Known_Diabetic
114 PCS119M 0.062654    0.000176    0.020358    New_Diagnosed
100 PCS11F  0.33044 0.000469    0.000352    New_Diagnosed
88  PCS120M 0.097811    0.000135    0   Normoglycemic
228 PCS125F 0.17703 0.000264    0.06429 Known_Diabetic
98  PCS127M 0.466902    0   0.011735    Normoglycemic
148 PCS130F 0.186682    0   0.000131    New_Diagnosed
233 PCS132F 0.003126    0   0   Known_Diabetic

I want to use lm function to plot the simple linear regression between Fasting_glucose with all other columns using Description column as a grouping variable. 我想使用lm函数来绘制使用描述列作为分组变量的Fasting_glucose与所有其他列之间的简单线性回归。

Currently, I am trying to use following script: 目前,我正在尝试使用以下脚本:

Prevotella<-ggplot(fasting.glucose, aes(Fasting_glucose, Prevotella)) +
geom_point() +
geom_smooth(method="lm")+ geom_point(aes(size = Fasting_glucose))+geom_point(aes(fill=Description, size=Fasting_glucose),  shape=21)+theme(panel.background = element_rect(fill='white', colour='black')) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

But it is producing only one plot at a time. 但是它一次只生产一个地块。

So just want to ask how can apply lm function and plot it for all the columns at once. 因此,只想问一下如何应用lm函数并一次为所有列绘制它。

You need to make your data tidy to use it with ggplot2. 您需要整理数据才能与ggplot2一起使用。 This means loading the reshape2 package and using the melt function. 这意味着装入reshape2包并使用melt功能。

library(ggplot2)
library(reshape2)

x <- read.table(text = "Fasting_glucose sample  Prevotella  Turicibacter    Mitsuokella Description
138 PCS119F 0.005782    0   0   Known_Diabetic
114 PCS119M 0.062654    0.000176    0.020358    New_Diagnosed
100 PCS11F  0.33044 0.000469    0.000352    New_Diagnosed
88  PCS120M 0.097811    0.000135    0   Normoglycemic
228 PCS125F 0.17703 0.000264    0.06429 Known_Diabetic
98  PCS127M 0.466902    0   0.011735    Normoglycemic
148 PCS130F 0.186682    0   0.000131    New_Diagnosed
233 PCS132F 0.003126    0   0   Known_Diabetic", header = TRUE)

y <- melt(x, id.vars = c("Fasting_glucose", "sample", "Description"))

ggplot(y, aes(Fasting_glucose, value, colour = Description)) + geom_point() +
geom_smooth(method = "lm") + facet_wrap(~ variable)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM