I have a df:
head(hej3)
Year variable N Sum Mean sd Variance Median Min Max
1 1901 Delägare.män. 85 18089 212.81176 365.39
To use your original data frame you should change to lines. In both calls to geom_line()
put linetype=
inside the aes()
and set the type to variable name.
+ geom_line(aes(y = Mean, color = "Medelvärde",linetype = "Medelvärde"),
size = 1.5, alpha = 1)
+ geom_line(aes(y = N,
color = "Antal Kassor",linetype="Antal Kassor"), size = 0.9, alpha = 1)
Then you should add scale_linetype_manual()
with the same name as for scale_colour_manual()
and there set line types you need.
+scale_linetype_manual("Variabler",values=c("Antal Kassor"=2,"Medelvärde"=1))
Also guides()
should be adjusted for linetype
and colours
to better show lines in legend.
+ guides(fill = guide_legend(keywidth = 1, keyheight = 1),
linetype=guide_legend(keywidth = 3, keyheight = 1),
colour=guide_legend(keywidth = 3, keyheight = 1))
Here is complete code used:
theplot<- ggplot(subset(hej3,variable=="Delägare.män."), aes(x = Year)) +
geom_line(aes(y = Mean, color = "Medelvärde",linetype = "Medelvärde"),
size = 1.5, alpha = 1) +
geom_ribbon(aes(ymax = Max,
ymin = Min, fill = "Delägare Män Max/Min"), linetype = 3,
alpha = 0.4) +
geom_ribbon(aes(ymax = Mean+sd, ymin = Mean-sd, fill = "Mean +- sd"),
colour = "grey50", linetype = 3, alpha = 0.8)+
#geom_line(aes(y = Sum,
#color = "Sum Delägare Män"), size = 0.9, linetype = 1, alpha = 1) +
geom_line(aes(y = N,
color = "Antal Kassor",linetype="Antal Kassor"), size = 0.9, alpha = 1)+
scale_y_continuous(breaks = seq(-500, 4800, by = 100), limits = c(-500, 4800),
labels = seq(-500, 4800, by = 100))+
scale_x_continuous(breaks=seq(1901,1930,2))+
labs(title = "Manliga Delägare i Yrkeskassor") +
scale_color_manual("Variabler", breaks = c("Antal Kassor","Medelvärde"),
values = c("Antal Kassor" = "black", "Medelvärde" = "#6E6E6E")) +
scale_fill_manual(" Ribbons", breaks = c("Delägare Män Max/Min", "Mean +- sd"),
values = c(`Delägare Män Max/Min` = "grey50", `Mean +- sd` = "#4E4E4E")) +
scale_linetype_manual("Variabler",values=c("Antal Kassor"=2,"Medelvärde"=1))+
theme(legend.direction = "horizontal", legend.position = "bottom", legend.key = element_blank(),
legend.background = element_rect(fill = "white", colour = "gray30")) +
guides(fill = guide_legend(keywidth = 1, keyheight = 1), linetype=guide_legend(keywidth = 3, keyheight = 1),
colour=guide_legend(keywidth = 3, keyheight = 1)) +
coord_cartesian(ylim = c(-300, 600))
Here you are want to change the linetype conditional on a variable. So, we create a new data set:
R> library(reshape2)
R> dd = melt(hej3, colnames(hej3)[c(1:2, 4, 6:10)])
R> dd = dd[dd$variable=="Delägare.män.",c(1, 9:10)]
R> head(dd, 4)
Year variable value
1 1901 N 85
3 1902 N 92
5 1903 N 99
7 1904 N 112
then we remove your two geom_line functions calls and replace with:
+ geom_line(data=dd, aes(x=Year, y = value, linetype=variable),
size = 1.5, alpha = 1) +
where the line type changes according to your variable.
To zoom into a region of you plot (help page)
+ coord_cartesian(ylim = c(-300, 600))
Full ggplot2 code for reference:
ggplot(subset(hej3,variable=="Delägare.män."), aes(x = Year)) +
geom_line(data=dd, aes(x=Year, y = value, linetype=variable),
size = 1.5, alpha = 1) +
geom_ribbon(aes(ymax = Max,
ymin = Min, fill = "Delägare Män Max/Min"), linetype = 3,
alpha = 0.4) +
geom_ribbon(aes(ymax = Mean+sd, ymin = Mean-sd, fill = "Mean +- sd"),
colour = "grey50", linetype = 3, alpha = 0.8)+
scale_color_manual("Variabler", breaks = c("Antal Kassor","Medelvärde"),
values = c("Antal Kassor" = "black", "Medelvärde" = "#6E6E6E")) +
scale_fill_manual(" Ribbons", breaks = c("Delägare Män Max/Min", "Mean +- sd"),
values = c(`Delägare Män Max/Min` = "grey50", `Mean +- sd` = "#4E4E4E")) +
theme(legend.direction = "horizontal", legend.position = "bottom", legend.key = element_blank(),
legend.background = element_rect(fill = "white", colour = "gray30")) +
guides(fill = guide_legend(keywidth = 0.9, keyheight = 1)) +
coord_cartesian(ylim = c(-300, 600))