ggplot2 custom legend shapes

别说谁变了你拦得住时间么 提交于 2019-12-28 06:35:51

问题


When we have both shape and color legend in scatter plot, the shape of color legend is misleading:

foo <- data.frame(
  length=runif(10),
  height=runif(10),
  group=as.factor(sample(3,10,rep=T)),
  quality=as.factor(sample(2,10,rep=T))
)

ggplot(foo, aes(x = length, y = height, color=group, shape=quality)) + 
  geom_point(size=5)

This will produce the plot below. As you see, the "circle" shape is reserved for the objects of quality==1, however in the group legend, all 3 groups are presented in "circle" shapes - with different colors, this can be misleading.

It was too much better if the group legend was represented by a shape not already reserved for a specific purpose, like just to fill the whole legend item with the specific color.

Do you have any simple idea how to solve this?


回答1:


It is possible to manually change the properties of the legend using guides:

ggplot(foo, aes(x = length, y = height, color=group, shape=quality)) + 
  geom_point(size=5) + 
  guides(colour = guide_legend(override.aes = list(shape = 15)))

Just play around with the shape paramater to find an appropriate shape.



来源:https://stackoverflow.com/questions/13456765/ggplot2-custom-legend-shapes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!