[R] lineplot in ggplot2 with different colour and linetype

0 views
Skip to first unread message

Harsh

unread,
Feb 18, 2009, 2:34:47 PM2/18/09
to r-h...@r-project.org
Hi list,
I would like to use ggplot2 in creating a line plot with 4 lines (groups), 2
of which I want in colour and the remaining two as dotted lines.

### R code ###
library(ggplot2)

### create data ####
vals <- rnorm(400)
div<- c(rep("A",100),rep("B",100),rep("C",100),rep("D",100))
n<- rep(1:100,4)
df<- data.frame(div= as.factor(div),vals=vals,n=n)

### ggplot function ####
qplot(x=n,y=vals, colour = div ,geom = "line",data=df)

What I would like to see is a line plot where I can have lines for groups A,
and B in colour and lines for C and D in dotted and dashed respectively.

Also, I would like to know how I can draw two lines (groups C and D) which
are both dotted.


Thanks
Regards
Harsh Singhal

[[alternative HTML version deleted]]

______________________________________________
R-h...@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.

Domenico Vistocco

unread,
Feb 18, 2009, 3:14:09 PM2/18/09
to Harsh, r-h...@r-project.org

Harsh wrote:
> Hi list,
> I would like to use ggplot2 in creating a line plot with 4 lines (groups), 2
> of which I want in colour and the remaining two as dotted lines.
>
> ### R code ###
> library(ggplot2)
>
> ### create data ####
> vals <- rnorm(400)
> div<- c(rep("A",100),rep("B",100),rep("C",100),rep("D",100))
> n<- rep(1:100,4)
> df<- data.frame(div= as.factor(div),vals=vals,n=n)
>
> ### ggplot function ####
> qplot(x=n,y=vals, colour = div ,geom = "line",data=df)
>
> What I would like to see is a line plot where I can have lines for groups A,
> and B in colour and lines for C and D in dotted and dashed respectively.
>
ggp <- ggplot(aes(x=n,y=vals, colour=div, linetype=div), data=df) +
geom_line()
ggp + scale_colour_manual(values = c("red","blue","black","black")) +
scale_linetype_manual(values = c(1,1,3,2))

> Also, I would like to know how I can draw two lines (groups C and D) which
> are both dotted.
>

For graphic specifications see Hadley' instructions:
http://had.co.nz/ggplot2/book/specifications.pdf

Ciao,
domenico

Reply all
Reply to author
Forward
0 new messages