更改复合图例标题 [英] Change composite legend title

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

问题描述

如果我有以下数据并使用 ggplot2 进行绘图,那么:

 <$ (1:5,0.9,0.9,0.9,0.9,0.9)
c < - c(0.3,0.3,0.3,0.3,0.3)
b< -c c(1:5,0.5,0.5,0.5,0.5,0.5)
z< -rep(1:5,5)
df< - data.frame(y = c(a,b ,c),x = c(z),line = c(rep(1,5),
rep(2,5),rep(3,5),rep(2 ),5),rep(3,5)))

library(ggplot2)

a < - ggplot(df,aes(x = x,y = y,fill = line,shape = line,group = line))+
geom_line(aes(linetype = line),size = 1)+
scale_linetype_manual(values = c(dashed,solid ),dotdash))+
geom_point(size = 3)+ scale_shape_manual(values = c(25,23,21,25,23))+
scale_fill_manual(values = c(red ,blue,yellow,red,blue))

指定图例的标题我可以做很多事情,如$ / $>

  a + labs(shape =我的标题在这里 )#或

a < - ggplot(df,aes(x = x,y = y,fill = line, (aes(linetype = line),size = 1)+
scale_linetype_manual(values = c(dashed,solid,dotdash)) )+
geom_point(size = 3)+ scale_shape_manual(values = c(25,23,21,25,23),name =MY
TITLE HERE)+
scale_fill_manual(values = c(红色,蓝色,黄色,红色,蓝色))


然而,所有这些选项都将复合图例分解为各自的映射参数。

如何使用 linetype shape 填充并更改图例标题? code> ggplot2 所有具有相同标签的比例将被组合在一起,因此您需要这样做:


  1. (可选)使用您的标签创建一个变量,例如 scale_label

  2. 将相同的标签作为第一个参数传递给每个比例。

例如:

  scale_label<  - 我的自定义标题

a < - ggplot(df,aes(x = x,y = y,fill = line,shape = line,group = line))+
geom_line(aes(linetype = line),size = 1)+
scale_linetype_manual(scale_label,values = c(dashed,solid,dotdash))+
geom_point(size = 3)+
scale_shape_manual(scale_label,values = c(25,23,21,25,23))+
scale_fill_manual(scale_label,values = c(red,blue,yellow,red,blue))
#scale_shape(Title)
print(a)


If I have the following data and plot it using ggplot2 I get:

a  <-c(0.3,0.3,0.3,0.3,0.3)
b  <-c(1:5,0.9,0.9,0.9,0.9,0.9)
c  <-c(1:5,0.5,0.5,0.5,0.5,0.5)
z  <-rep(1:5,5)
df <- data.frame(y=c(a,b,c),x=c(z),line=c(rep("1",5),
rep("2",5),rep("3",5),rep("2",5),rep("3",5)))

library(ggplot2)

a <- ggplot(df,aes(x=x,y=y,fill=line,shape=line,group=line)) +       
          geom_line(aes(linetype=line),size=1) +            
     scale_linetype_manual(values=c("dashed","solid", "dotdash")) +
          geom_point(size=3) + scale_shape_manual(values=c(25,23,21,25,23)) +    
     scale_fill_manual(values=c("red", "blue", "yellow","red", "blue"))

If I want to specify the title of the legend I can do a number of things like

a + labs(shape = "MY TITLE HERE")   # or

a <- ggplot(df,aes(x=x,y=y,fill=line,shape=line,group=line)) +       
          geom_line(aes(linetype=line),size=1) +            
     scale_linetype_manual(values=c("dashed","solid", "dotdash")) +
          geom_point(size=3) + scale_shape_manual(values=c(25,23,21,25,23),name="MY 
          TITLE HERE") +    
     scale_fill_manual(values=c("red", "blue", "yellow","red", "blue"))

However, all of these options break the composite legend up into their separate mapping paramters.

How do I maintain the composite legend with linetype,shape and fill and change the legend title?

解决方案

In ggplot2 all scales with the same label will be grouped together, so you need to do this:

  1. (optional) Create a variable with your label, e.g. scale_label
  2. Pass the same label to each scale as the first argument.

For example:

scale_label <- "My custom title"

a <- ggplot(df,aes(x=x,y=y,fill=line,shape=line,group=line)) +       
    geom_line(aes(linetype=line),size=1) +            
    scale_linetype_manual(scale_label, values=c("dashed","solid", "dotdash")) +
    geom_point(size=3) + 
    scale_shape_manual(scale_label, values=c(25,23,21,25,23)) +    
    scale_fill_manual(scale_label, values=c("red", "blue", "yellow","red", "blue")) 
    #scale_shape("Title")
print(a)

这篇关于更改复合图例标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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