Resize / reorder one manual in the ggplot combined manual

I find a combined legend that the shape and type of a line is more difficult to decipher. in particular, the shape is hard to see because it is beyond the line and it is too small.

library(ggplot2)
ggplot(mtcars)+
geom_point(aes(x=hp,y=mpg,shape=as.factor(cyl)))+
geom_smooth(aes(x=hp,y=mpg,linetype=as.factor(cyl)),method='lm')+
theme_bw(base_size=18)

How to increase the size of figures in a legend without increasing the size of the line?

this attempt below increases the size for both (not what i want). the order guide_legendalso does not affect the order of characters in the symbol keys. Reordering geom_point and geom_smooth will give the desired result in the legend, but not on the chart.

+guides(linetype=guide_legend('Cylinders'),shape=guide_legend('Cylinders',override.aes=list(size=3)))

I also hoped to theme(legend.key.size=grid::unit(2,'cm'))increase the size of the objects in the legend key, but it doesn't seem to.

offers? also open to other ideas on how to make the schedule more understandable.

+4
1

, , , , :

ggplot(mtcars)+
  geom_smooth(aes(x=hp,y=mpg,linetype=as.factor(cyl)),method='lm')+
  geom_point(aes(x=hp,y=mpg,shape=as.factor(cyl)))+
  theme_bw(base_size=18)

enter image description here

. , , :

library(gtable)
library(gridExtra)

# Has the legend you want
p1 <- ggplot(mtcars)+
  geom_smooth(aes(x=hp,y=mpg,linetype=as.factor(cyl)),method='lm')+
  geom_point(aes(x=hp,y=mpg,shape=as.factor(cyl)),size=3)+
  theme_bw(base_size=18)+labs(shape="Cylinders",linetype="Cylinders")

# Has the plot you want
p2 <- ggplot(mtcars)+
  geom_smooth(aes(x=hp,y=mpg,linetype=as.factor(cyl)),method='lm')+
  geom_point(aes(x=hp,y=mpg,shape=as.factor(cyl)))+
  theme_bw(base_size=18)+theme(legend.position="none")

# Take the legend from p1
fill.legend <- gtable_filter(ggplot_gtable(ggplot_build(p1)), "guide-box") 
legGrob <- grobTree(fill.legend)

# Put the legend from p1 onto p2
grid.arrange(p2, legGrob, ncol=2, widths = c(6,1))

enter image description here

+2

Source: https://habr.com/ru/post/1619251/


All Articles