I tried to draw a ggplot
with below data and aesthetics
library(ggplot2)
mydata = rbind(data.frame('date' = as.Date(c('2010-01-01', '2011-01-01', '2012-01-01')), var1 = c('A', 'A', 'A'), var2 = c('X', 'X', 'X'), val = c(10, 3, 10)),
data.frame('date' = as.Date(c('2010-01-01', '2011-01-01', '2012-01-01')), var1 = c('B', 'B', 'B'), var2 = c('X', 'X', 'X'), val = c(10+2, 3+2, 10+2)),
data.frame('date' = as.Date(c('2010-01-01', '2011-01-01', '2012-01-01')), var1 = c('A', 'A', 'A'), var2 = c('Y', 'Y', 'Y'), val = c(10-2, 3-2, 10-2)),
data.frame('date' = as.Date(c('2010-01-01', '2011-01-01', '2012-01-01')), var1 = c('B', 'B', 'B'), var2 = c('Y', 'Y', 'Y'), val = c(10+5, 3+5, 10+5)))
ggplot(data = mydata, aes(x = date)) +
geom_line(aes(y = val, color = var1)) +
geom_point(aes(y = val, color = var1, shape = var2))
This approach is generating a strange plot. I wanted to draw 4 different lines on same date
in x-axis, where var1
to be distinguished by colour and var2
to be distinguished by point shape.
Is there any way to achieve this?