R ggplot2更改*特定*线型图例的背景颜色 [英] R ggplot2 change background color of *specifically* linetype legend

查看:92
本文介绍了R ggplot2更改*特定*线型图例的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是一个类似的问题,但是该解决方案不适用于 scale_linetype_manual :

当我使用 legend.key 参数时,这就是我得到的(我将其设置为黑色,因此它是可见的,即 legend.key = element_rect(fill =#000000)):

如您所见,它在灰色的矩形后面 添加了一个黑色的矩形.

如何更改灰色矩形的颜色?

这是ggplot2代码:

  p = ggplot()+主题(axis.line.y.right = element_line(color =#FF00FF"),axis.ticks.y.right = element_line(color =#FF00FF"),axis.text.y.right = element_text(color =#FF00FF"),axis.title.y.right = element_text(color =#FF00FF"),panel.background = element_rect(fill ="white",color ="black"),legend.key = element_rect(fill =#000000"))+geom_rect(data = df2,mapping = aes(xmin = xmin,xmax = xmax,ymin = ymin,ymax = ymax,fill = color),show.legend = T)+scale_fill_manual(name =密码子类型",values = c('#FFEBCC','#E6FFF7','#F2F2F2'),labels = c("CR","NCR","None"),限制= c("CR","NCR",无"))+#-----这应该是相关的部分-----geom_path(data = df,aes(x = resseqnum,y = y.sec,color ='b',linetype ='b'))+geom_path(data = df,aes_string(x ='resseqnum',y ='cen',color ='a',linetype ='a'),size = .5)+geom_path(aes(x = c(x.min,x.max),y = c(a,a),color ='c',linetype ='c'))+scale_linetype_manual(name ="Lines",values = c("a" ='solid',"b" ='solid',"c" ='dotted'),labels = c('Centrality','Minmax','Zero reference'))+scale_color_manual(name ="Lines",values = c('#00FF00','#FF00FF','#FF00FF'),labels = c('Centrality','Minmax','Zero reference'))+#-----相关部分的可能结尾-----scale_y_continuous("Centrality",sec.axis = sec_axis(〜(.-a)/b,name ="Minmax"))+scale_x_continuous(序列位置")+coord_cartesian(xlim = c(0,10))+ggtitle(标题") 

为了重现性:

df.txt

  resseqnum,cen,y.sec1,4,32,5,63,6,34,NA,NA5,NA,NA6,6,47,3,28,1,39,5,310,10,5 

df2.txt

  xmin,xmax,ymin,ymax,颜色1,2,1,10,CR5,7,1,10,NCR9,10,1,10,无 

其他:

  b = 2.25a = 5 

解决方案

您需要添加

  guides(linetype = guide_legend(override.aes = list(fill =#000000"))) 

在您的情节中,您将得到类似以下内容的东西:

Here is a similar question, but the solution does not work for scale_linetype_manual: Similar but not the same SO question. When I use the legend.key parameter it only inserts a element_rect behind the lines in my scale. (See pictures below)

I have everything working on my graph now except that my graph has a white background and scale_linetype_manual is inserting a grey background.

When I use the legend.key parameter this is what I get (I set it to black so it would be visible, i.e., legend.key=element_rect(fill="#000000")):

As you can see, it is adding a black rectangle behind the grey one.

How can I change the color of the grey rectangle?

Here is the ggplot2 code:

p = ggplot()+
    theme(axis.line.y.right = element_line(color = "#FF00FF"), 
          axis.ticks.y.right = element_line(color = "#FF00FF"),
          axis.text.y.right = element_text(color = "#FF00FF"), 
          axis.title.y.right = element_text(color = "#FF00FF"),
          panel.background = element_rect(fill="white",color="black"),
          legend.key=element_rect(fill="#000000"))+

    geom_rect(data=df2, mapping=aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,fill=color),show.legend = T)+
    scale_fill_manual(name="Codon type",values=c('#FFEBCC','#E6FFF7','#F2F2F2'),
                      labels=c("CR","NCR","None"),
                      limits=c("CR","NCR","None"))+

    #-----This should be the relevant part-----
    geom_path(data=df,aes(x=resseqnum,y=y.sec,color='b',linetype='b'))+
    geom_path(data=df,aes_string(x='resseqnum',y='cen',color='a',linetype='a'),size=.5)+
    geom_path(aes(x=c(x.min,x.max),y=c(a,a),color='c',linetype='c'))+
    scale_linetype_manual(name="Lines",values=c("a"='solid',"b"='solid',"c"='dotted'),
                          labels=c('Centrality','Minmax','Zero reference'))+
    scale_color_manual(name="Lines",values=c('#00FF00','#FF00FF','#FF00FF'),
                       labels=c('Centrality','Minmax','Zero reference'))+
    #-----Possible end of relevant part-----    

    scale_y_continuous("Centrality",sec.axis=sec_axis(~ (. - a)/b,name="Minmax"))+
    scale_x_continuous("Sequence location")+
    coord_cartesian(xlim=c(0,10))+

    ggtitle("Title")

For reproducibility:

df.txt

resseqnum,cen,y.sec
1,4,3
2,5,6
3,6,3
4,NA,NA
5,NA,NA
6,6,4
7,3,2
8,1,3
9,5,3
10,10,5

df2.txt

xmin,xmax,ymin,ymax,color
1,2,1,10,CR
5,7,1,10,NCR
9,10,1,10,None

Other:

b=2.25
a=5

解决方案

You need to add

guides(linetype = guide_legend(override.aes = list(fill = "#000000")))

To your plot, then you'll get something like this:

这篇关于R ggplot2更改*特定*线型图例的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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