How to change line type

34 views
Skip to first unread message

lily li

unread,
Feb 4, 2015, 12:10:09 PM2/4/15
to ggp...@googlegroups.com
Dear ggplot users,

I want to ask that how to change the line type? That is to say, if I plot the two lines on one chart, but I want to have one in dashed line and one in solid line. The code below doesn't work. Thanks for your help.

p1=ggplot()+
  geom_path(data=climateData, aes(x=date,y=temp,color='Temperature'))+
  geom_path(data=climateData, aes(x=date,y=precip,color='Precipitation'))+
  xlab('Day of Year 1960')+ylab('Daily climate parameters')+
  scale_color_manual('',breaks=c('Temperature','Precipitation'),values=c('blue','red'))+
  scale_linetype_manual(values=c('dashed','solid'))
show(p1)

climateData
date            temp   precip
1960-9-9      52           0.2
1960-9-10    52           0
1960-9-11    58           0
...

Ben Bond-Lamberty

unread,
Feb 4, 2015, 12:56:42 PM2/4/15
to ggp...@googlegroups.com
Have you read the basic ggplot documentation? Basically, you want your
data to be in 'long' format, not 'wide', and then map the `linetype`
aesthetic to a variable. That's by far the easiest way to do this.

For example, given your data set:

library(reshape2)
climateData_long <- melt(climateData, id.vars="date")
ggplot(climateData_long, aes(date, value, linetype=variable)) + geom_line()

Ben
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

lily li

unread,
Feb 4, 2015, 1:49:03 PM2/4/15
to Ben Bond-Lamberty, ggp...@googlegroups.com
Thanks, Ben.
I'm not very clear of melt function, but I think my question is how to change the line type. For example, I want to plot two variables on the same plot, one is in solid line, one is in dashed line.

Brandon Hurr

unread,
Feb 4, 2015, 1:52:35 PM2/4/15
to lily li, Ben Bond-Lamberty, ggp...@googlegroups.com
http://docs.ggplot2.org/current/scale_linetype.html

Ben's reply should give you what you want with a legend for the two types of data. melt() is part of the reshape2 or tidyr package. Look at climateData and climateData_long in his example and you should see how the data is reformatted. 

B

gullix

unread,
Feb 5, 2015, 11:12:48 AM2/5/15
to ggp...@googlegroups.com
Hi Lily Li,

ggplot 2 does not allow making plots with two Y-axes. Having said that it is certainly possible to make a plot with both temp and precip on the same plot, although I don't think it will be pretty or informative. The temp variable seems to vary around 50, while precip seems vary from 0-1. So once you make the plot you may be able to see the trend in temp, but changes in precip may look like a straight line. To resolve this you will need two Y-axes on the same plot (which is not allowed), or make two separate plots for temp and precip.

If you still want to make one plot with temp and precip as two different lines, I would suggest you look at this website called Cookbook for R (http://www.cookbook-r.com/). Look at Manipulating data/Restructuring data to change your data frame to a long format. Basically your final dataset should have three columns, one with the dates, second with factor (temp or precip) and the third with the measurements. Also look at the Graphs section to define a certain linetype for each variable (temp or precip).

Hope this helps.

Kartick.
Reply all
Reply to author
Forward
0 new messages