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?
Thanks in advanced
--
Pablo de Camargo Cerdeira
pablo.cerdeira@gmail.com
+55 (21) 3799-6065
[[alternative HTML version deleted]]
Hello,
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.
#install.packages("ggplot2")
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 c("CONTRACTS",
"FAMILY", "INTERNATIONAL", "TAXATION"), class
"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)) +
geom_line()
HTH,
Josh
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:
>
> 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?
>
> 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
http://www.joshuawiley.com/
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
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.<snip>> 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 you link to an example of the type of graphical output you'd expect?
> require(lattice)Loading required package: lattice> xyplot(CASES ~ YEAR | AREA, data = t, type = "b") > xyplot(CASES ~ YEAR, data = t, type = "b", groups = AREA)-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Pablo Cerdeira Sent: Wednesday, 30 June 2010 10:42 AM To: r-help at r-project.org Subject: [R] Multiline and grouping in R 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? 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.
Hi:
This is to add a couple of bells and whistles to the excellent replies of
David and Bill. You may not want the 0.5 increments in the x-axis ticks, and
if you want all groups in a single plot, then you may want a legend. Below
are some basic ways to specify these (to head off the obvious follow-up
questions :)
# (1) Individual panels per AREA:
xyplot(CASES ~ YEAR|AREA, data=df, type="b",
scales = list(x = list(at = c(1988, 1989, 1990))))
The scales = argument provides control over various aspects of the axes,
such as labels and tick marks. In this case, we want to limit the x-axis
ticks to occur at 1988, 1989 and 1990 only.
# (2) Single graph with multiple AREA profiles over time:
xyplot(CASES ~ YEAR, data = df, type = "b", groups = AREA,
scales = list(x = list(at = c(1988, 1989, 1990))),
auto.key = list(points = FALSE, lines = TRUE))
The auto.key = argument is used to produce a simple legend. By default, it
is listed on top of the plot; if, for example, you wanted it on the right
instead, you could add space = 'right' to the auto.key list.
There are **many** options in xyplot() and other Lattice graphics functions,
so you have the capability of fine tuning a graph to meet your
specifications.
HTH,
Dennis
On Tue, Jun 29, 2010 at 5:42 PM, Pablo Cerdeira
<pablo.cerdeira@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:
>
> 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?
>
> Thanks in advanced
>
> --
> Pablo de Camargo Cerdeira
> pablo.cerdeira@gmail.com
> +55 (21) 3799-6065
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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]]
On 06/30/2010 10:42 AM, 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. >Hi Pablo, Just to give you another option, if your data frame is "pcdf": library(plotrix) newpcmat<-cbind(pcdf$CASES[pcdf$YEAR==1988], pcdf$CASES[pcdf$YEAR==1989], pcdf$CASES[pcdf$YEAR==1990]) colnames(newpcmat)<-c("1988","1989","1990") rownames(newpcdf)<-c("CONTRACTS","INTERNATIONAL","FAMILY","TAXATION") bumpchart(newpcmat,col=1:4,rank=FALSE, main="Number of cases by year") boxed.labels(rep(2.5,3),c(400,300,200),c(400,300,200)) Jim