You can use your version to also get the correct linetype, if you put the linetype insidt the aes
function. However, your code becomes even more cumbersome that way. Consider reshaping your data before calling ggplot
. Then you don't have to worry about the legend at all.
# reshape data ...
new_data$Ta <- Ta
new_data$zero <- 0
require(reshape2)
dta <- melt(new_data, id.vars="V", measure.vars=c("IlD", "IlL", "Ta"))
dta.lower <- melt(new_data, id.vars="V", measure.vars=c("IlD_fill", "zero", "Ta"))
dta.upper <- melt(new_data, id.vars="V", measure.vars=c("Ta", "IlL", "Ta"))
dta <- cbind(dta, lower=dta.lower$value, upper=dta.upper$value)
dta$name <- factor(NA, levels=c("Illegal landfill owner's\nprofitable ratio\n",
"Waste owner's\nprofitable ratio",
"Official tax"))
dta$name[dta$variable=="IlD"] <- "Illegal landfill owner's\nprofitable ratio\n"
dta$name[dta$variable=="IlL"] <- "Waste owner's\nprofitable ratio"
dta$name[dta$variable=="Ta"] <- "Official tax"
Now tha plotting command becomes much easier and more transparent:
ggplot(dta, aes(x=V, y=value, ymin=lower, ymax=upper,
color=name, fill=name, linetype=name)) +
geom_line(size=1.2) + ylim(c(0, Ta*1.5)) +
geom_ribbon(alpha=.25, linetype=0) +
theme(axis.text.x = element_text(angle=0, hjust = 0),
axis.title = element_text(face = 'bold', size = 14),
title = element_text(face = 'bold', size = 16),
legend.position = 'right',
legend.title = element_blank(),
legend.text = element_text(size = 12),
legend.key.width = unit(2, 'cm'))+
scale_linetype_manual(values=c(4, 5, 1)) +
labs(title="Profitable ratio between the volume \nof illegally disposed waste \nand costs of illegal waste disposure",
x="Waste volume, cubic meters",
y="Cost per cubic meter, RUB")