Re: [R] Multiline and grouping in R

From: David Winsemius <dwinsemius_at_comcast.net>
Date: Tue, 29 Jun 2010 21:14:14 -0400

On Jun 29, 2010, at 8:42 PM, Pablo Cerdeira 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:
>
> YEAR,AREA,CASES
> 1988,CONTRACTS,286
> 1988,INTERNATIONAL,189
> 1988,FAMILY,385
> 1988,TAXATION,177
> 1989,CONTRACTS,233
> 1989,INTERNATIONAL,431
> 1989,FAMILY,425
> 1989,TAXATION,201
> 1990,CONTRACTS,190
> 1990,INTERNATIONAL,302
> 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?

 > txt <-textConnection("YEAR,AREA,CASES

+ 1988,CONTRACTS,286
+ 1988,INTERNATIONAL,189
+ 1988,FAMILY,385
+ 1988,TAXATION,177
+ 1989,CONTRACTS,233
+ 1989,INTERNATIONAL,431
+ 1989,FAMILY,425
+ 1989,TAXATION,201
+ 1990,CONTRACTS,190
+ 1990,INTERNATIONAL,302
+ 1990,FAMILY,303
+ 1990,TAXATION,209")

 > t <- read.csv(txt, header=TRUE)
 > t
    YEAR          AREA CASES
1  1988     CONTRACTS   286
2  1988 INTERNATIONAL   189
3  1988        FAMILY   385
4  1988      TAXATION   177
5  1989     CONTRACTS   233
6  1989 INTERNATIONAL   431
7  1989        FAMILY   425
8  1989      TAXATION   201
9  1990     CONTRACTS   190
10 1990 INTERNATIONAL   302
11 1990        FAMILY   303
12 1990      TAXATION   209
 > require(lattice)
 > help(package=lattice)

 > xyplot(CASES ~ YEAR|AREA, data=t)
 > ?xyplot
starting httpd help server ... done
 > xyplot(CASES ~ YEAR|AREA, data=t, type="l")  > xyplot(CASES ~ YEAR, group=AREA, data=t, type="l")
-- 

David Winsemius, MD
West Hartford, CT

______________________________________________
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.
Received on Wed 30 Jun 2010 - 01:17:36 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 30 Jun 2010 - 01:20:43 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive