Quantcast

Boxplot of mean, sd, max, min without raw data

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Boxplot of mean, sd, max, min without raw data

dangerousfrizbee
I am trying to make a boxplot using already calculated mean, sd, min and max values (I don't have access to the raw data, but I want to convert a table to boxplots). I tried to use the geom_boxplot command in ggplot2 with stat="identity", but it is not working (I think it only works on raw data?) - if I enter my data in a dataframe (df) and then enter the following commands:
ggplot(df, aes(x))+ geom_boxplot(aes(ymin = y1, lower = y2, middle = y3, upper = y4, ymax = y5), stat = "identity")

I get an empty plot with axis labels (this makes me think it only works on raw data with built in calculations?)

Is there a way to do what I want to do?

I have uploaded my data file.WCM4.txt

thanks!
Susan

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Boxplot of mean, sd, max, min without raw data

affableambler
This post was updated on .
Do you want a separate box plot for each row?  If so, try this:

library(ggplot2)

#Read in the data:
data<-read.table('E:\\Libraries\\Documents\\WCM4.txt',sep='\t',header=T,stringsAsFactors = F)

#Remove column of row numbers
data$x<-NULL

#Run a for loop to create separate boxplots for the data in each row:
for(row in 1:nrow(data)){boxplot(as.numeric(as.vector(data[row,])))}

Feel free to add additional arguments to boxplot() as needed to achieve your desired output format.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Boxplot of mean, sd, max, min without raw data

dangerousfrizbee
thanks, that does work, but I actually want to plot all of the data in one plot, labelled 1-14 along the x axis. any further suggestions would be much appreciated!

On 12 April 2017 at 22:49, affableambler [via R] <[hidden email]> wrote:
Do you want a separate box plot for each row?  If so, try this:

library(ggplot2)

#Read in the data:
data<-read.table('E:\\Libraries\\Documents\\WCM4.txt',sep='\t',header=T,stringsAsFactors = F)

#Remove column of row numbers
data$x<-NULL

#Run a for loop to create separate boxplots for the data in each row:
for(row in 1:ncol(data)){boxplot(as.numeric(as.vector(data[row,])))}

Feel free to adjust the parameters of boxplot() as needed.


If you reply to this email, your message will be added to the discussion below:
http://r.789695.n4.nabble.com/Boxplot-of-mean-sd-max-min-without-raw-data-tp4730939p4731631.html
To unsubscribe from Boxplot of mean, sd, max, min without raw data, click here.
NAML

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Boxplot of mean, sd, max, min without raw data

affableambler
Sorry for not getting back to you sooner. In that case, you can use par() before the loop to combine multiple plots into one view:

data<-read.table('E:\\Libraries\\Documents\\WCM4.txt',sep='\t',header=T,stringsAsFactors = F)

#Remove column of row numbers
data$x<-NULL

#Run a for loop to create separate boxplots for the data in each row:
par(mfrow=c(1,nrow(data)))
for(row in 1:nrow(data)){boxplot(as.numeric(as.vector(data[row,])),main=row)}


The mfrow parameter takes a vector of two integers.  The first is the number of rows in the view and the second is the number of columns, so the code above gives you one row with fourteen plots.  If you want to divide it into two rows, just change it to c(2,nrow(data)/2), etc.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Boxplot of mean, sd, max, min without raw data

dangerousfrizbee
Hi,

Thanks for your further advice. However, I must not have explained myself very clearly, sorry about that. I want one plot with all of the boxplots in it plotted using the same Y scale so that I can visually compare them to each other. Does that make sense?  See attached sketch that will hopefully make it clearer!

Sorry again for not being clear from the outset as to what I was trying to do!

kind regards,
Susan

On 13 April 2017 at 23:19, affableambler [via R] <[hidden email]> wrote:
Sorry for not getting back to you sooner. In that case, you can use par() before the loop to combine multiple plots into one view:

data<-read.table('E:\\Libraries\\Documents\\WCM4.txt',sep='\t',header=T,stringsAsFactors = F)

#Remove column of row numbers
data$x<-NULL

#Run a for loop to create separate boxplots for the data in each row:
par(mfrow=c(1,nrow(data)))
for(row in 1:nrow(data)){boxplot(as.numeric(as.vector(data[row,])),main=row)}


The mfrow parameter takes a vector of two integers.  The first is the number of rows in the view and the second is the number of columns, so the code above gives you one row with fourteen plots.  If you want to divide it into two rows, just change it to c(2,nrow(data)/2), etc.




If you reply to this email, your message will be added to the discussion below:
http://r.789695.n4.nabble.com/Boxplot-of-mean-sd-max-min-without-raw-data-tp4730939p4732123.html
To unsubscribe from Boxplot of mean, sd, max, min without raw data, click here.
NAML


boxplot.jpg (67K) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Boxplot of mean, sd, max, min without raw data

affableambler
This post was updated on .
Hi Susan,

This will require reshaping the data:

data<-read.table('E:\\Libraries\\Documents\\WCM4.txt',sep='\t',header=T,stringsAsFactors = F)

library(reshape2)

#Melt the data into three columns with 70 rows:
melted_data<-melt(data,id.vars = 1, measure.vars = 2:6)

#Plot using ggplot2
library(ggplot2)
plot<- ggplot(data = melted_data, aes(x=x, y=value))
plot <- plot + stat_boxplot(geom ='errorbar',aes(group=x))  
plot <- plot + geom_boxplot(aes(group=x))
plot <- plot + scale_x_continuous(breaks = 1:14, labels = 1:14)
plot(plot)

I also noticed that in rows 2 and 14, the "upper" is greater than "ymax."  Maybe the data was entered incorrectly?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Boxplot of mean, sd, max, min without raw data

dangerousfrizbee
thank you so much!! works like a charm and I would never have figured it out on my own

On 18 April 2017 at 16:46, affableambler [via R] <[hidden email]> wrote:
Hi Susan,

This will require reshaping the data:

data<-read.table('E:\\Libraries\\Documents\\WCM4.txt',sep='\t',header=T,stringsAsFactors = F)

library(reshape2)

#Melt the data into three columns with 70 rows:
melted_data<-melt(data,id.vars = 1, measure.vars = 2:6)

#Plot using ggplot2
library(ggplot2)
plot<- ggplot(data = melted_data, aes(x=x, y=value))
plot <- plot + stat_boxplot(geom ='errorbar',aes(group=x))  
plot <- plot + geom_boxplot(aes(group=x))
plot <- plot + scale_x_continuous(breaks = 1:14, labels = 1:14)

I also noticed that in rows 2 and 14, the "upper" is greater than "ymax."  Maybe the data was entered incorrectly?


If you reply to this email, your message will be added to the discussion below:
http://r.789695.n4.nabble.com/Boxplot-of-mean-sd-max-min-without-raw-data-tp4730939p4732696.html
To unsubscribe from Boxplot of mean, sd, max, min without raw data, click here.
NAML



--
Susan M Miller

Postdoctoral Fellow
FitzPatrick Institute of African Ornithology
DST-NRF Centre of Excellence
University of Cape Town
Rondebosch 7701
South Africa
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Boxplot of mean, sd, max, min without raw data

affableambler
My pleasure Susan.  I had never used the reshape package before so it was a good learning opportunity for me as well.  Glad I could help.

Noah
Loading...