user3526009
user3526009

Reputation: 33

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.

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.

Upvotes: 0

Views: 82

Answers (1)

nacnudus
nacnudus

Reputation: 6528

You need to make your data tidy to use it with ggplot2. This means loading the reshape2 package and using the melt function.

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)

Upvotes: 1

Related Questions