ePoQ
ePoQ

Reputation: 463

Linetype on a time serie plot (ggplot)

I would like to apply different linetype models on timeseries plot. Here is a reproducible example using a structure similar to my data/code. Let's say I want solid line for woman and dotted line for men.

source("http://www.openintro.org/stat/data/arbuthnot.R")
library(ggplot2)
library(reshape2)

names(arbuthnot) <- c("Year", "Men", "Women")

arbuthnot.melt <- melt(arbuthnot, id.vars = 'Year', variable.name = 'Sex', 
                       value.name = 'Rate')
ggplot(arbuthnot.melt, aes(x = Year, y = Rate, shape = Sex, color = Sex))+
  geom_line() + scale_color_manual(values = c("Women" = '#ff00ff','Men' = '#3399ff')) + 
  scale_linetype_manual(values = c('Women' = 'solid', 'Men' = 'dotted'))

I have been stuck here a long time, I tried other syntaxs such as c(0,4) , scale_linetype_manual(values = c('Women' = 1, 'Men' = 4)), scale_shape_manual, etc. I really don't understand why scale_linetype_manual is not working here. Any help would be greatly appreciated.

Upvotes: 2

Views: 55

Answers (1)

coffeinjunky
coffeinjunky

Reputation: 11514

Try

ggplot(arbuthnot.melt, aes(x = Year, y = Rate, shape = Sex, color = Sex, linetype = Sex))+
  geom_line() + scale_color_manual(values = c("Women" = '#ff00ff','Men' = '#3399ff')) + 
  scale_linetype_manual(values = c('Women' = 'solid', 'Men' = 'dotted'))

enter image description here

The linetype is part of the aesthetic mapping, so it needs to be included in the aes-element.

Upvotes: 1

Related Questions