Albert Vilella
2010-Apr-24 19:53 UTC
[R] left-to-right series of box and whisker plots from a csv file
Hi, I've got a csv file with scores like this: 40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,32,38,40,20,40,26,40,30,40,5,5,13,12,40,40,3,33,29,23,2,24,9,15,4,21,16,5,26,8,8,18 40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,32,40,40,30,40,40,37,34,40,36,13,40,13,40,5,40,8,29,5,15,34,5,22,14,20 40,40,40,40,40,40,40,40,40,12,40,40,40,20,40,40,17,40,40,40,40,40,11,40,40,29,12,40,1,13,21,10,14,24,9,13,18,28,19,12,15,3,5,3,8,2 [...] For a given file, the number of columns can vary from a few dozen to a few hundreds, and I would like a script that draws a series of box and whiskers or violins left to right for every column in a single plot. How can I do that in a self-contained R script that can be called with the input file as argument? Cheers, Albert.
David Winsemius
2010-Apr-24 20:44 UTC
[R] left-to-right series of box and whisker plots from a csv file
On Apr 24, 2010, at 3:53 PM, Albert Vilella wrote:> Hi, > > I've got a csv file with scores like this: > 40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,32,38,40,20,40,26,40,30,40,5,5,13,12,40,40,3,33,29,23,2,24,9,15,4,21,16,5,26,8,8,18 > 40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,32,40,40,30,40,40,37,34,40,36,13,40,13,40,5,40,8,29,5,15,34,5,22,14,20 > 40,40,40,40,40,40,40,40,40,12,40,40,40,20,40,40,17,40,40,40,40,40,11,40,40,29,12,40,1,13,21,10,14,24,9,13,18,28,19,12,15,3,5,3,8,2 > [...] > > For a given file, the number of columns can vary from a few dozen to a > few hundreds, and I would like a script that > draws a series of box and whiskers or violins left to right for every > column in a single plot. > > How can I do that in a self-contained R script that can be called with > the input file as argument? > > Cheers, > > Albert. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD West Hartford, CT
Dennis Murphy
2010-Apr-25 03:02 UTC
[R] left-to-right series of box and whisker plots from a csv file
Hi:
Here are a few options. The key step, it seems to me, is to transform the
data so that
you have one 'factor' consisting of the variable names and another
column
with
the values; this is what the melt() function in the reshape package does.
This idea
should work irrespectively of the number of variables in the data frame; the
only
condition is that they are all numeric...and of course, comparable.
library(ggplot2) # adds several packages, including reshape
library(lattice)
# Manufacture some fake data with five numeric variables to compare
df <- data.frame(x1 = rnorm(20), x2 = rnorm(20), x3 = rnorm(20),
x4 = rnorm(20), x5 = rnorm(20))
dfmelt <- melt(df)
# Basically unlists the variables into a vector value and adds an indicator
of
# the variable to which each value belongs...
dim(dfmelt)
# [1] 100 2
names(dfmelt) # variable is the 'factor', value is a numeric vector
# [1] "variable" "value"
# Generate some plots...
# Vertical boxplot in ggplot2
ggplot(dfmelt, aes(x = variable, y = value)) + geom_boxplot()
last_plot() + xlab('These are my variables') + ylab('These are my
values')
# Horizontal - just add coord_flip() to previous call:
last_plot() + coord_flip()
# Horizontal violin plot in ggplot2:
p <- ggplot(dfmelt, aes(value))
p + geom_ribbon(aes(ymax = ..density.., ymin = -..density..),
stat = 'density') + facet_grid(variable ~
.)
# Vertical and horizontal boxplots and violin plots in lattice:
bwplot(value ~ variable, data = dfmelt)
bwplot(value ~ variable, data = dfmelt, panel = panel.violin)
bwplot(variable ~ value, data = dfmelt)
bwplot(variable ~ value, data = dfmelt, panel = panel.violin)
HTH,
Dennis
On Sat, Apr 24, 2010 at 12:53 PM, Albert Vilella <avilella@gmail.com>
wrote:
> Hi,
>
> I've got a csv file with scores like this:
>
>
40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,32,38,40,20,40,26,40,30,40,5,5,13,12,40,40,3,33,29,23,2,24,9,15,4,21,16,5,26,8,8,18
>
>
40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,32,40,40,30,40,40,37,34,40,36,13,40,13,40,5,40,8,29,5,15,34,5,22,14,20
>
>
40,40,40,40,40,40,40,40,40,12,40,40,40,20,40,40,17,40,40,40,40,40,11,40,40,29,12,40,1,13,21,10,14,24,9,13,18,28,19,12,15,3,5,3,8,2
> [...]
>
> For a given file, the number of columns can vary from a few dozen to a
> few hundreds, and I would like a script that
> draws a series of box and whiskers or violins left to right for every
> column in a single plot.
>
> How can I do that in a self-contained R script that can be called with
> the input file as argument?
>
> Cheers,
>
> Albert.
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]