使用表达式标签时,结合填充和颜色图例 [英] Combine fill and colour legends when using expression labels

查看:172
本文介绍了使用表达式标签时,结合填充和颜色图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在图例标签中使用子/上标。但是,如果我使用相应比例的标签参数,则颜色和填充比例不再组合。



有没有方法可以解决这个问题或者在传说中使用子/上标的方法?



<$ p $ (x = 1:10,y =(1:10)^ 2,
ymax =(1:10)^ 2.2,
ymin =(1:10)^ 1.8,
fac = rep(c(a,b),each = 5))

library(ggplot2)
p < - ggplot(DF,aes(x = x,y = y,ymin = ymin,ymax = ymax,color = fac,fill = fac))+
geom_line()+
geom_ribbon = 0.5)

print(p)


  p + scale_color_discrete(labels = c(a= expression(a [{foo}]),
b= expression(b [{bar}])))+
scale_fill_discrete(labels = c( (b [{bar}])))


解决方案

不是很优雅的解决方案是删除 fill 图例,然后使用 override.aes = guides()>函数中为 color = 图例。对于这个传说,我们可以设置自己的 fill = 值。唯一的问题是你必须知道颜色名称。我认为使用 scale_color_manual()会更容易,因为您已经提供了自己的颜色值。

  p + scale_color_discrete(labels = c(a= expression(a [{foo}]),
b= expression(b [{bar}]))+
scale_fill_discrete(guide =none)+
guides(color = guide_legend(override.aes = list(fill = c(#F8766D,#00BFC4))))


I need to use sub/superscripts in legend labels. However, if I use the label parameter of the respective scales, the colour and the fill scale are not combined anymore.

Is there a way to fix this or a different way to use sub/superscripts in legends?

DF <- data.frame(x=1:10,y=(1:10)^2,
                 ymax=(1:10)^2.2,
                 ymin=(1:10)^1.8,
                 fac=rep(c("a","b"),each=5))

library(ggplot2)
p <- ggplot(DF,aes(x=x,y=y,ymin=ymin,ymax=ymax,colour=fac,fill=fac)) +
  geom_line() +
  geom_ribbon(alpha=0.5)

print(p)

p +  scale_color_discrete(labels=c("a"=expression(a[{foo}]),
                                   "b"=expression(b[{bar}]))) +
     scale_fill_discrete(labels=c("a"=expression(a[{foo}]),
                                  "b"=expression(b[{bar}]))) 

解决方案

Not so elegant solution is to remove fill legend and then use override.aes= within guides() function for color= legend. For this legend we can set our own fill= values. Only problem is that you have to know color names. I think this would be easier with scale_color_manual() because you already provide your own color values.

p +  scale_color_discrete(labels=c("a"=expression(a[{foo}]),
                                   "b"=expression(b[{bar}]))) +
  scale_fill_discrete(guide="none")+
  guides(color=guide_legend(override.aes=list(fill=c("#F8766D","#00BFC4"))))

这篇关于使用表达式标签时,结合填充和颜色图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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