使用表达式标签时

使用表达式标签时

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

问题描述

我需要在图例标签中使用子/上标。但是,如果我使用相应比例的

不是很优雅的解决方案是删除 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"))))

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

08-31 10:02