I would like to make some plots from my data. Unfortunately, it is hard to predict how many plots I will generate because it depends on data and may be different. It is a reason why I would like to make it easy adjustable. However, it will be most often a plot from group of 3 rows each time.
So, I would like to plot from rows 1:3, 4-6,7-9, etc.
This is data:
> dput(DF_final)
structure(list(AC = c(0.0031682160632777, 0.00228591145206846,
0.00142094444568728, 0.000661218113472149, 0.0010078157353918,
0.000400289437089513, 40.4634784175177, 40.5055070858594, 0.0183737773741582
), SD = c(0.00250647379467532, 0.0013244185401148, 0.000469332241199189,
0.000294558308707343, 0.000385553400676202, 0.000104447914881357,
11.0693842400794, 8.78768774254084, 0.00696532251341454), ln_AC = c(-5.75458660556339,
-6.08099044923792, -6.556433525855, -7.32142679754668, -6.89996992823399,
-7.8233226797995, 3.70039979980691, 3.70143794229703, -3.99683077355773
), ln_SD = c(-5.98887837626238, -6.62678175351058, -7.66419963690747,
-8.13003358225542, -7.86083085139947, -9.16682203300101, 2.40418312097106,
2.17335162163583, -4.96681136795312), Percent_AC = c(126.401324043689,
172.597361244303, 302.758754023937, 224.477834753288, 261.394591157605,
383.243109777925, 365.544076706723, 460.934756361151, 263.789326894369
), Percent_SD = c(100, 100, 100, 100, 100, 100, 100, 100, 100
), TP = c(0, 40, 80, 0, 40, 80, 0, 40, 80)), row.names = c("Tim_0",
"Tim_40", "Tim_80", "Jack_0", "Jack_40", "Jack_80", "Tom_0",
"Tom_40", "Tom_80"), class = "data.frame")
Column ln_AC should be set as an Y axis and column TP as X axis. First of all I would like to have all of them on separate graphs next to each other (remember about issue that the number of plots may be igh at some point) and if possible everything at the same graph. It should be a point plot with trend line.
Is it also possible to get a slope, SD slope, R^2 on a plot from linear regression ?
I manage to do it a for a single plot but regression line looks strange...
The code below was used to generate this plot and regression line.
fit <- lm(DF_final$ln_AC~DF_final$TP, data=DF_final)
plot(DF_final[1:3,7], DF_final[1:3,3], type = "p", ylim = c(-10,0), xlim=c(0,100), col = "red")
lines(DF_final$TP, fitted(fit), col="blue")
In base R (without so many packages), you can do:
# splits every 3 rows
DF = split(DF_final,gsub("_[^ ]*","",rownames(DF_final) ))
# you can also do
# DF = split(DF_final,(1:nrow(DF_final) - 1) %/%3 ))
To store your values:
slopes = vector("numeric",3)
names(slopes) = names(DF)
rsq = vector("numeric",3)
names(rsq) = names(DF)
To plot:
par(mfrow=c(1,3))
for(i in names(DF)){
fit <- lm(ln_AC~TP, data=DF[[i]])
plot(DF[[i]]$TP, DF[[i]]$ln_AC, type = "p", col = "red",main=i)
abline(fit, col="blue")
slopes[i]=round(fit$coefficients[2],digits=2)
rsq[i]=round(summary(fit)$r.squared,digits=2)
mtext(side=1,paste("slope=",slopes[i],"\nrsq=",rsq[i]),
padj=-2,cex=0.7)
}
And your values:
slopes
Jack Tim Tom
-0.01 -0.01 -0.10
rsq
Jack Tim Tom
0.29 0.99 0.75
If I understand correctly, the reason you want 3 observation per graph is because you have different individuals (Jack,Tim,Tom) . Is that so?
If you don't want to worry about that number, you can do this
# move rownames to column
data$person <- rownames(data)
data$person <- gsub("\\_.*","",data$person) # remove TP from names
# better to use library(data.table) for this step
data <- melt(data,id.vars=c("person","TP","ln_AC"))
ggplot(data,aes(x=TP, y=ln_AC)) + geom_point() +
geom_smooth(method = "lm") + facet_grid(~person)
This results in a plot like #giocomai, but it will work also if you have 4,5,6 or whatever persons in your data.
---- Edit
If you want to add R2 values, you can do something like this. Note, that it may not be the best and elegant solution, but it works.
data <- data.frame(...)
data$person <- rownames(data)
data$person <- gsub("\\_.*","",data$person)
# run lm for all persons and save them in a data.frame
nomi <- unique(data$person)
#lmStats <- data.frame()
lmStats <- sapply(nomi,
function(ita){
model <- lm(ln_AC~TP,data= data[which(data$person == ita),])
lmStat <- summary(model)
# I only save r2, but you can get all the statistics you need
lmRow <- data.frame("r2" = lmStat$r.squared )
#lmStats <- rbind(lmStats,lmRow)
}
)
lmStats <- do.call(rbind,lmStats)
# format the output,and create a dataframe we will use to annotate facet_grid
lmStats <- as.data.frame(lmStats)
rownames(lmStats) <- gsub("\\..*","",rownames(lmStats))
lmStats$person <- rownames(lmStats)
colnames(lmStats)[1] <- "r2"
lmStats$r2 <- round(lmStats$r2,2)
lmStats$TP <- 40
lmStats$ln_AC <- 0
lmStats$lab <- paste0("r2= ",lmStats$r2)
# melt and add r2 column to the data (not necessary, but I like to have everything I plot in teh data)
data <- melt(data,id.vars=c("person","TP","ln_AC"))
data$r2 <- lmStats[match(data$person,rownames(lmStats)),1]
ggplot(data,aes(x=TP, y=ln_AC)) + geom_point() +
geom_smooth(method = "lm") + facet_grid(~person) +
geom_text(data=lmStats,label=lmStats$lab)
An easier way (less steps) would be to use facet_grid(~r2), so that you have the R.square value in the title.
If I understand correctly what you mean, assuming you will always have three observation per graph, your main issue would be creating a categorical variable to separate them. Here's one way to accomplish it. Depending on the layout you prefer, you may want to check facet_wrap instead of facet_grid.
library("dplyr")
library("ggplot2")
DF_final <- structure(list(AC = c(0.0031682160632777, 0.00228591145206846,
0.00142094444568728, 0.000661218113472149, 0.0010078157353918,
0.000400289437089513, 40.4634784175177, 40.5055070858594, 0.0183737773741582
), SD = c(0.00250647379467532, 0.0013244185401148, 0.000469332241199189,
0.000294558308707343, 0.000385553400676202, 0.000104447914881357,
11.0693842400794, 8.78768774254084, 0.00696532251341454), ln_AC = c(-5.75458660556339,
-6.08099044923792, -6.556433525855, -7.32142679754668, -6.89996992823399,
-7.8233226797995, 3.70039979980691, 3.70143794229703, -3.99683077355773
), ln_SD = c(-5.98887837626238, -6.62678175351058, -7.66419963690747,
-8.13003358225542, -7.86083085139947, -9.16682203300101, 2.40418312097106,
2.17335162163583, -4.96681136795312), Percent_AC = c(126.401324043689,
172.597361244303, 302.758754023937, 224.477834753288, 261.394591157605,
383.243109777925, 365.544076706723, 460.934756361151, 263.789326894369
), Percent_SD = c(100, 100, 100, 100, 100, 100, 100, 100, 100
), TP = c(0, 40, 80, 0, 40, 80, 0, 40, 80)), row.names = c("Tim_0",
"Tim_40", "Tim_80", "Jack_0", "Jack_40", "Jack_80", "Tom_0",
"Tom_40", "Tom_80"), class = "data.frame")
DF_final %>%
mutate(id = as.character(sapply(1:(nrow(DF_final)/3), rep, 3))) %>%
ggplot(aes(x=TP, y=ln_AC)) +
geom_point() +
geom_smooth(method = "lm") +
facet_grid(~id)
Created on 2020-02-06 by the reprex package (v0.3.0)
iarray <- iv$iarray
varray <- iv$varray
n<-gsub("^\\{+(.+)\\}+$", '\\1', iarray)
n1 <- strsplit(n,",")
n1 <- unlist(n1)
n1 <- as.numeric(n1)
df <- as.data.frame(n1)
n<-gsub("^\\{+(.+)\\}+$", '\\1', varray)
n2 <- strsplit(n,",")
n2 <- unlist(n2)
n2 <- as.numeric(n2)
df <- cbind(df,n2)
vmpp <-iv$vmpp
impp <- iv$impp
print(impp)
print(vmpp)
})
output$ivcurve <- renderPlot({
ggplot(data3(), aes(x=n2, y= n1)) + geom_line(colour='blue')+ geom_vline(xintercept = vmpp)+ geom_hline(yintercept = impp) + scale_y_continuous(limits = c(-1, 11))
Basically I'm trying to draw an IV curve from the above code.
As seen in the photo I need a horizontal and a vertical line.
But after I added the geom_vline function it gives me the Error : ggplot2 doesn't know how to deal with data of class numeric
iv is a dataframe and iarray and varray basically looks like this.
iarray = "{9.467182035,9.252423958,9.179368178,9.142931845}"
varray = "{-1.025945126,-0.791203874,-0.506481774,-0.255416444}"
And vmpp and impp are basically numbers as 8.5 and 20
suggestions?
P.s :
dput(iv)
structure(list(id = 3L, seris_id = "SERTPTR0003", module_id = 2L,
isc = 9.1043, voc = 37.61426, impp = 8.524, vmpp = 30.0118,
pmpp = 255.8095, unique_halm_id = 4414L, iarray = "{9.471385758,9.251831868,9.174032904,9.135095327,9.109244512,9.087563112,9.081257993,9.079282455,9.078209387,9.077396672,9.076717653,9.076285598,9.075914058,9.075549594,9.075098675,9.074659768,9.074080201,9.073659578,9.073411255,9.073349331,9.073215686,9.073189667,9.073011759,9.072868405,9.072659064,9.072636165,9.072659725,9.072729724,9.072779321,9.072915415,9.072951718,9.072855259,9.072758863,9.072562734,9.072286497,9.072036161,9.071858009,9.07165223,9.071458902,9.071172024,9.070818323,9.070364851,9.069865071,9.069392026,9.069058847,9.068673155,9.068486996,9.0684006,9.068241175,9.067848351,9.067533806,9.066886103,9.066177782,9.0655086,9.065025577,9.064457111,9.064154995,9.063866251,9.063564149,9.063221961,9.06295813,9.062580288,9.062182005,9.06179715,9.061378517,9.060847632,9.06033015,9.059686156,9.058814993,9.057817299,9.056732355,9.055534236,9.054389596,9.05351149,9.052819766,9.052254696,9.051816304,9.051431465,9.051000987,9.050664797,9.050589584,9.050615635,9.050795719,9.051096084,9.05121704,9.050958132,9.050478383,9.049724325,9.048695951,9.047619756,9.046715916,9.04602525,9.045615278,9.045512729,9.045617691,9.045803509,9.045989974,9.046083526,9.045997615,9.045871618,9.045772357,9.045599926,9.045340971,9.045082036,9.04473025,9.044178732,9.043440888,9.042642632,9.04185002,9.041056695,9.040316091,9.039781509,9.039426971,9.039199774,9.039026035,9.038805897,9.038478843,9.037978051,9.037190302,9.036262611,9.035408047,9.034687132,9.03411323,9.033759457,9.033445779,9.033105372,9.032611665,9.031991392,9.031298017,9.030631384,9.029991493,9.02931152,9.028518372,9.027678053,9.026644378,9.025384369,9.023971135,9.022443918,9.020510444,9.018469233,9.015987042,9.013123551,9.009951782,9.006524239,9.002508657,8.99806541,8.993200713,8.987509287,8.980851319,8.97337198,8.964883202,8.955065215,8.944015742,8.931773812,8.91796823,8.902911552,8.886450605,8.868452754,8.848678419,8.827119435,8.80336248,8.777313996,8.748941051,8.718309497,8.685225063,8.649388501,8.610785476,8.569040812,8.52363426,8.474699468,8.422382481,8.366516735,8.307103187,8.244481209,8.178090447,8.10779633,8.033345875,7.954744415,7.871665908,7.784296593,7.692116999,7.595199333,7.493377787,7.386704971,7.275055109,7.158981607,7.038484468,6.913650942,6.784728642,6.651977027,6.515069048,6.374111623,6.228897233,6.079031999,5.924669253,5.766323899,5.604063459,5.43841477,5.26939121,5.096619936,4.919752772,4.738936722,4.554312451,4.366039658,4.174017769,3.978461295,3.779470133,3.576724216,3.370764477,3.162238756,2.951119622,2.737359938,2.521133452,2.302407806,2.08132299,1.858467726,1.632539296,1.397202225,1.149523324,0.890812319,0.62251893,0.349040094,0.084409259,-0.164612445,-0.4001423,-0.625408177,-0.844927296,-1.067373925,-1.297998987,-1.536777099,-1.782558235,-2.033692207,-2.28906274,-2.54694712,-2.806836154,-3.068463186,-3.331653821,-3.596227332,-3.862303417,-4.129421924,-4.397321356,-4.666082505,-4.935632162,-5.206170796,-5.478105728,-5.751638617,-6.027203502,-6.304753878,-6.584235675,-6.865027697,-7.146774939,-7.428922534,-7.711971427,-7.995982555,-8.281623641,-8.569128828,-8.85847189,-9.14887768,-9.440152159,-9.731968139,-10.02382391,-10.315645796,-10.608918155,-10.906228043,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}",
varray = "{-1.055634971,-0.820094649,-0.530478984,-0.277519378,-0.049665975,0.168173928,0.369832037,0.557189853,0.73806136,0.918444007,1.100988955,1.285835111,1.471379381,1.656087228,1.83947039,2.021804885,2.204138782,2.387586314,2.572217234,2.757544476,2.943083961,3.127927125,3.311936258,3.49497066,3.677517995,3.860273388,4.043516446,4.227247167,4.411953813,4.597148124,4.781785019,4.965795342,5.149247651,5.331933288,5.514618924,5.698279889,5.882706331,6.067759345,6.253369179,6.43883951,6.623542572,6.807967224,6.991834459,7.175283184,7.359219574,7.543715171,7.727930567,7.91102934,8.092315166,8.270881273,8.44728269,8.622987785,8.800575578,8.981370755,9.16634984,9.355446065,9.546982405,9.738937256,9.930334092,10.119987137,10.30698723,10.492242934,10.676242509,10.859335313,11.042009008,11.224684494,11.40735998,11.589966311,11.77250289,11.955040067,12.138134659,12.321927365,12.506347836,12.691464628,12.877626501,13.06357852,13.248553416,13.432761044,13.616063692,13.798391012,13.981067688,14.165071441,14.350401073,14.536497974,14.722526917,14.907441627,15.090542195,15.272247132,15.453463208,15.634886746,15.817842429,16.00302897,16.188982779,16.375285347,16.561309505,16.745870074,16.92840904,17.110739948,17.293002899,17.47610287,17.660388619,17.84523298,18.029659429,18.213737119,18.397257992,18.580849811,18.765279846,18.950546303,19.136368979,19.322329963,19.507382979,19.691527431,19.874834264,20.057444179,20.239565832,20.422106592,20.605414621,20.789699174,20.974891096,21.161129891,21.347647095,21.533745789,21.718937711,21.903640773,22.087576567,22.271163603,22.454611733,22.638548722,22.822346805,23.006144888,23.189873219,23.373392294,23.556911967,23.740989056,23.925624756,24.110540657,24.296084919,24.481558832,24.666267268,24.850416495,25.034286715,25.217528572,25.400211222,25.582891485,25.765153238,25.947554494,26.130303912,26.31319522,26.495810505,26.678077627,26.859158373,27.03919344,27.2183891,27.396813913,27.574889968,27.752475972,27.92950277,28.106320312,28.283627309,28.460794206,28.638101203,28.814990286,28.99097379,29.16472524,29.336732302,29.506996765,29.675866194,29.843757906,30.011789121,30.179820337,30.347501601,30.514694006,30.680631478,30.844614709,31.006505389,31.166790586,31.32526224,31.482546323,31.638644628,31.793695462,31.947491958,32.10059034,32.252713392,32.40427843,32.555147743,32.705110284,32.853817294,33.001129867,33.14656034,33.29010692,33.431979459,33.572526716,33.711957348,33.85055096,33.988306953,34.125295678,34.260890565,34.395089819,34.528311952,34.660559355,34.792040088,34.923102908,35.053748414,35.1835569,35.312946278,35.441359731,35.568936163,35.695816274,35.821859963,35.946717873,36.071016573,36.194685715,36.317446291,36.439648854,36.561153301,36.681402816,36.801235017,36.924136292,37.050595501,37.180333637,37.313071096,37.446575824,37.573873847,37.693009727,37.80489203,37.910636184,38.012195237,38.11396235,38.218102215,38.324333435,38.432308447,38.541817399,38.652023867,38.762158191,38.872501769,38.982844151,39.092768022,39.202552988,39.31199099,39.420452469,39.528287977,39.635566071,39.742146647,39.848308713,39.954401627,40.060004483,40.165396887,40.270857247,40.376317008,40.481775571,40.587234732,40.692485836,40.797320225,40.901805855,41.005874173,41.109873339,41.21366265,41.317242107,41.420542558,41.523772657,41.626762446,41.729471102,41.832232456,41.937530675,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}"), .Names = c("id",
"seris_id", "module_id", "isc", "voc", "impp", "vmpp", "pmpp",
"unique_halm_id", "iarray", "varray"), row.names = 1L, class = "data.frame")
Solved it. since the ggplot requires data frames. I just added two more columns to the data frame and added them there.
vmpp <- iv$vmpp
df <- cbind(df,vmpp)
impp <- iv$impp
df <- cbind(df,impp)
print(df)
})
output$ivcurve <- renderPlot({
ggplot(data3(), aes(x=n2, y= n1)) + geom_line(colour='blue')+ scale_y_continuous(limits = c(-1, 11))+ geom_vline(aes(xintercept = vmpp))+ geom_hline(aes(yintercept = impp))