[R] Multiline and grouping in R

Joshua Wiley jwiley.psych at gmail.com
Wed Jun 30 03:00:39 CEST 2010


There are many ways to do what you want in R.  I am showing one way
using one of my favorite graphics packages, ggplot2.  If you do not
have it installed yet, uncomment the first line.

library(ggplot2) #load the package

#Read in data
samp.dat <- structure(list(YEAR = c(1988L, 1988L, 1988L, 1988L, 1989L,
1989L, 1989L, 1989L, 1990L, 1990L, 1990L, 1990L), AREA =
structure(c(1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L), .Label =
"factor"), CASES = c(286L, 189L, 385L, 177L, 233L, 431L, 425L, 201L,
190L, 302L, 303L, 209L)), .Names = c("YEAR", "AREA", "CASES"), class =
"data.frame", row.names = c(NA, -12L))

ggplot(data=samp.dat, aes(x=YEAR, y=CASES, group=AREA, colour=AREA)) +



On Tue, Jun 29, 2010 at 5:42 PM, Pablo Cerdeira
<pablo.cerdeira at gmail.com> wrote:
> Hi All,
> this is my first mail here.
> I'm trying to plot a multiline chart grouping values with no success. I have
> read a lot in the official Wiki and also searched via Google, but I did not
> find anything.
> I'm importing some data from a cvs file. Here is a sample:
> 1988,CONTRACTS,286
> 1988,FAMILY,385
> 1988,TAXATION,177
> 1989,CONTRACTS,233
> 1989,FAMILY,425
> 1989,TAXATION,201
> 1990,CONTRACTS,190
> 1990,FAMILY,303
> 1990,TAXATION,209
> ...
> "t <- read.csv("file.csv", header=TRUE)"
> So far so good...
> But the problem is: I'd like to create a multiline plot, one line per AREA,
> showing the evolution of the number of CASES per YEAR.
> I know how to do it in Excel, using a Pivot Table. But I'm trying hard to do
> the same with R but I have no idea on how to do it.
> Can someone help me?
> Thanks in advanced
> --
> Pablo de Camargo Cerdeira
> pablo.cerdeira at gmail.com
> +55 (21) 3799-6065
>        [[alternative HTML version deleted]]
> ______________________________________________
> 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

More information about the R-help mailing list