[R] Boxplots from data.frame

Brian Diggs diggsb at ohsu.edu
Wed Jul 7 22:27:55 CEST 2010


On 7/7/2010 1:11 PM, Ian Bentley wrote:
> Hi all,
>
> I'm trying to use ggplot to make a boxplot of some data, but I can't seem to
> figure out how to make it use the data I'm giving it.
>
> The data is in a data.frame so that it has two columns:
>
>> meltl
> value L1
> 1234  1
> 1234  1
> 1235  1
> ...
> 1255  1
> 2335  2
> 3444  2
> ...
> 10001 50
> 12311 50
> ...
>
> The first column is my x value, the second is my y.
>
> I'd like to produce one boxplot for each point on the graph, 50 in total
> (for this case).  When I try something like:
>
> p<- ggplot(meltl, aes(L1, value))
> p + geom_boxplot()
>
> I get one giant boxplot, which tells me nothing much.
>
> Can anyone give me a helping hand?

You are getting one boxplot because ggplot sees L1 is a continuous 
variable, and so creates just one box, but covering the range of L1. 
You need to let ggplot know that you consider L1 to be an indicator of 
category membership, rather than a continuous value.  Either of the 
following should work (untested since I don't have your meltl):

ggplot(meltl, aes(factor(L1), value)) + geom_boxplot()

This version turns L1 into a factor (categorical variable), so when 
ggplot creates boxplots, it does so for each level of the used factor(s).

ggplot(meltl, aes(L1, value, group=L1)) + geom_boxplot()

Here, L1 is still a continuous variable, but when ggplot goes to make 
boxplots, it is told to do so for each unique value of L1 separately. 
The two are not identical (first has a discrete x axis, the second a 
continuous one), but are very similar.

--
Brian Diggs
Senior Research Associate, Department of Surgery, Oregon Health & 
Science University



More information about the R-help mailing list