Reputation: 1109
I have a data consisting of 14 different time periods where I would like to plot it in a way that viewer can see where the 14 periods lie. I used to achieve this through using different colors
mycolors = c(brewer.pal(name="Set2", n = 7), brewer.pal(name="Set2", n = 7))
ggplot(derv, aes(x=Date, y=derv, colour = Season)) +
geom_point() +
geom_abline(intercept = 0, slope = 0) +
geom_abline(intercept = neg.cut, slope = 0) +
geom_abline(intercept = pos.cut, slope = 0) +
scale_color_manual(values = mycolors) + ggtitle(" Derivative")+ylab("Derivative")
I have used the above code to product such as plot but now in a new report, I can only use black and white scheme. So I am wondering how I can plot such a plot in R. I have thought of using alternating line types for the 14 different time periods but I do not how to achieve through ggplot. I have tried the following code, but it does not work.The line type stayed the same.
ggplot(derv, aes(x=Date, y=derv)) +
geom_line() +
geom_abline(intercept = 0, slope = 0) +
geom_abline(intercept = neg.cut, slope = 0) +
geom_abline(intercept = pos.cut, slope = 0) +
#scale_color_manual(values = mycolors) + ggtitle("S&P 500 (Smoothed) Derivative") + ylab("Derivative")+
scale_linetype_manual(values = c("dashed","solid","dashed","solid","dashed","solid","dashed",
"solid","dashed","solid","dashed","solid","dashed","solid"))
Upvotes: 1
Views: 101
Reputation: 93821
If you need to show where season changes, couldn't you just use an alternating linetype or alternating point marker? See below for two examples. You can play around with different point markers and linetypes to get the look you want. For more on creating linetypes, see this SO answer. For more on additional point markers (beyond the standard one available through pch
), see, for example, here and here. I've also included a way to add the three horizontal lines with less code.
# Fake data
x = seq(0,2*pi,length.out=20*14)
dat=data.frame(time=x, y=sin(x) + sin(5*x) + cos(2*x) + cos(7*x),
group=0:(length(x)-1) %/% 20)
ggplot(dat, aes(time, y)) +
geom_hline(yintercept=c(-0.5,0,0.5), colour="grey50") +
geom_point(aes(shape=factor(group), size=factor(group))) +
scale_shape_manual(values=rep(c(3,15),7)) +
scale_size_manual(values=rep(c(2,1.5),7)) +
theme_bw() + guides(shape=FALSE, size=FALSE)
ggplot(dat, aes(time, y, linetype=factor(group))) +
geom_hline(yintercept=c(-0.5,0,0.5), colour="grey50") +
geom_line(size=0.8) +
scale_linetype_manual(values=rep(1:2,7)) +
theme_bw() + guides(linetype=FALSE)
Upvotes: 1