[R] Splitting data in to multiple boxplots

Joshua Wiley jwiley.psych at gmail.com
Tue Sep 28 18:31:24 CEST 2010


Hi,

There is a very handy feature of boxplot() that will handle this
easily.  You can write formulae of the form:
scores ~ groups

For your sample data:
# read in data
dat <- read.table(textConnection("
id  cat  value
1   a    12
2   a    23
3   a    14
4   b    2
5   b    3
6   c    9
7   c    8
8   c    10
9   d    30
"), header = TRUE)
closeAllConnections()

# this should give you boxplots by cat, also note the data argument
# which tells it where to look for the variable names
boxplot(value ~ cat, data = dat)

HTH,

Josh

On Tue, Sep 28, 2010 at 9:24 AM, deadlyspider <wrcstone at gmail.com> wrote:
>
> Hi,
>
> I have a data set in the following format:
>
>
> id  cat  value
> 1   a    12
> 2   a    23
> 3   a    14
> 4   b    2
> 5   b    3
> 6   c    9
> 7   c    8
> 8   c    10
> 9   d    30
>
>
> I would like to set up boxplots for each category. The actual category names
> are long and many so I would like this to be split automatically. Is this
> possible? Can anybody point me in the right direction for this?
>
> Thanks.
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Splitting-data-in-to-multiple-boxplots-tp2717491p2717491.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.



--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list