我的数据如下所示:
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
我想使用lm
函数绘制Fasting_glucose
与所有其他列之间的简单线性回归,使用描述列作为分组变量。
目前,我正在尝试使用以下脚本:
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())
但它一次只生产一个地块。
所以我想问一下如何应用lm
函数并一次为所有列绘制它。
答案 0 :(得分:1)
您需要将数据tidy与ggplot2一起使用。这意味着加载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)