Reputation: 227
I would like add dotted lines and black segment to my legend for the next plot:
structure(list(Species = structure(c(2L, 4L, 1L, 5L, 3L, 6L), .Label = c("Lithobates catesbeianus", "Eleutherodactylus coqui", "Rhinella marina", "Eleutherodactylus planirostris", "Polypedates leucomystax", "Xenopus laevis"), class = "factor", scores = structure(c(`Eleutherodactylus coqui` = -153579482.74, `Eleutherodactylus planirostris` = -4270489.16, `Lithobates catesbeianus` = -6040180271.32, `Polypedates leucomystax` = -169747.4, `Rhinella marina` = -76676305.87, `Xenopus laevis` = -75965.79), .Dim = 6L, .Dimnames = list(c("Eleutherodactylus coqui", "Eleutherodactylus planirostris", "Lithobates catesbeianus", "Polypedates leucomystax", "Rhinella marina", "Xenopus laevis")))), Costs = c(153579482.74, 4270489.16, 6040180271.32, 169747.4, 76676305.87, 75965.79), Million = c(153.5, 4.27, 6040.18, 0.17, 76.67, 0.075), Observed = c(18.29, 4.27, 3.95, 0.17, 43.52, 0.075)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame")).
The script that i am using is this :
p1 <- ggplot(data = b,
aes(Species, Million)) + ylab("Costs in Million US$")+xlab("Species") + scale_y_continuous(trans = "log10") + geom_segment(aes(xend=Species, yend=Million, y=0)) + theme_classic() + geom_segment(aes(xend=Species,yend= Observed, y =0, colour="red")) + theme(axis.text.x=element_text(angle=45,hjust=1,vjust=1)) + geom_point( size=3, color="black") + geom_hline(yintercept=1045.81, linetype="dashed", color = "green") + geom_hline(yintercept= mean(b$Observed), linetype="dashed", color = "blue")+ scale_color_discrete(c("Costs Million US$"), labels= "Observed costs")
I appreciate all the help
Upvotes: 0
Views: 42
Reputation: 2949
Bring line type and colour, inside aes and assign the line types for each.
p1 <- ggplot(data = b,
aes(Species, Million)) +
ylab("Costs in Million US$")+xlab("Species") +
scale_y_continuous(trans = "log10") +
geom_segment(aes(xend=Species, yend=Million, y=0, color="black",
linetype = "black")) +
theme_classic() +
geom_segment(aes(xend=Species,yend= Observed, y =0, color="red",
linetype = "red")) +
theme(axis.text.x=element_text(angle=45,hjust=1,vjust=1)) +
geom_point(color="black", size=3, ) +
geom_hline(aes(color = "green", yintercept=1045.81,
linetype = "green")) +
geom_hline(aes(color = "blue", yintercept = mean(Observed),
linetype="blue")) +
scale_color_manual(values = c("red" = "red", "black" = "black",
"green" = "green", "blue" = "blue"),
labels = c("red" = "Observed costs", "black" = "black segment",
"green" = "green line", "blue" = "blue line"),
"Costs Million US$") +
scale_linetype_manual(values = c("red" = "solid", "black" = "solid",
"green" = "dashed", "blue" = "dashed"),
labels = c("red" = "Observed costs", "black" = "black segment",
"green" = "green line", "blue" = "blue line"),
"Costs Million US$")
Upvotes: 1