ggplot2自定义图例形状 [英] ggplot2 custom legend shapes

查看:1528
本文介绍了ggplot2自定义图例形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  foo< p>如果我们在散点图中同时具有形状和颜色图例,那么颜色图例的形状会令人误解。 -  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)

这会产生如下图所示。正如你所看到的那样,circle形状是为 quality == 1 的对象保留的,但是在 group 传说中,所有3组都以圆形形状呈现 - 具有不同的颜色,这可能会产生误导。



如果



解决方案

可以使用指南手动更改图例的属性:

  ggplot(foo,aes(x = length,y = height,color = group, (color = guide_legend(override.aes = list(shape = 15)))

只要玩弄 shape 参数可以找到合适的形状。


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?

解决方案

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.

这篇关于ggplot2自定义图例形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆