I have attempted anything I could find but to no avail.
I have replicated my issue using the built in dataset mtcars. I am making a scatter plot with bubble chart characteristics with ggplot2 in R. A few things in my code that don't work with this data set are a log scale and forcing the colors of the points, but unless the order of those is a factor I think I can deal with that later.
What happened:
I have to have a 1:1 line, I used geom_abline as it seemed to make the most sense. I used methods I found from other questions to add the legend to the figure, but when that happens all the other legend entries change shape and orientation.
Ideally I would like to fix this problem and make the dashed line display in a horizontal fashion if possible.
p <- ggplot(mtcars, aes(drat,wt, size=hp, colour=cyl)) +geom_point()+
scale_size_area(max_size=5)+
labs(x="drat", y="wt")
p+ theme(panel.background = element_rect(fill = "white", colour = "black"),
panel.grid.major = element_line(colour= "grey75"),
axis.text.x = element_text(colour = "black"),
axis.text.y = element_text(colour = "black"))+
guides(colour = guide_legend(override.aes = list(size=6)))+
geom_abline(aes(intercept = 0, slope = 1, linetype = "dashed"),show_guide = TRUE)+
scale_linetype_manual("1:1 line", values = 2)
UPDATE: To solve the issue with the diagonal rectangles instead of circle points see Didzis Elferts solution below. Still looking for a way to make the legend line horizontal instead of on the diagonal like it is now. Code with Didzis Elferts fix added is below
p <- ggplot(mtcars, aes(drat,wt, size=hp, colour=cyl)) +geom_point()+
scale_size_area(max_size=5)+
labs(x="drat", y="wt")
p+ theme(panel.background = element_rect(fill = "white", colour = "black"),
panel.grid.major = element_line(colour= "grey75"),
axis.text.x = element_text(colour = "black"),
axis.text.y = element_text(colour = "black"))+
guides(colour = guide_legend(override.aes = list(size=6,
linetype=0)),size = guide_legend(override.aes=list(linetype=0)))+
geom_abline(aes(intercept = 0, slope = 1, linetype = "dashed"),
show_guide = TRUE)+scale_linetype_manual("1:1 line", values = 2)