【3.1】ggplot2--legend图例的修改

ggplot2中的legend包括四个部分:legend.tittle, legend.text, legend.key,legend.backgroud。

legend

一、针对每一部分有四种处理方式

  1. element_text()绘制标签和标题,可控制字体的family, face, colour, size, hjusvjust, angle, lineheight,当改变角度时,序将hjust调整至0或

  2. element_rect()绘制主要供背景使用的矩形,你可以控制颜色的填充(fill)和边界的colo size, linetype

  3. element_blank()表示空主题,即对元素不分配相应的绘图空间。该函数可以山区我们不感兴趣的绘图元素。使用之前的colour=NA,fill=NA,让某些元素不可见,但仍然占绘图空间。

  4. element_get()可得到当前主题的置theme()可在一幅图中对某些元素进行局部 性修改,theme_update()可为后面图形的绘制进行全局性的修

    head(msleep) name genus vore order conservation sleep_total sleep_rem sleep_cycleake brainwt bodywt 1 Cheetah Acinonyx carni Carnivora lc 12.1 NA NA 11.9 NA 50.000 2 Owl monkey Aotus omni Primates 17.0 1.8 NA 7.0 0.0150 0.480 3 Mountain beaver Aplodontia herbi Rodentia nt 14.4 2.4 NA 9.6 1.350 4 Greater short-tailed shrew Blarina m Soricomorpha lc 14.9 2.3 0.1333333 9.1 0.00029 0.019 5 Cow Bos herbi Artiodactyla domesticated 4.0 0.7 0.6666667 20.0 0.42300 600.000 6 Three-toed sloth Bradypus herbi Pilosa 14.4 2.2 0.7666667 9.6 NA 3.8

作图

p<-ggplot(data = msleep, aes(x=sleep_total,y= sleep_cycle,colour = vore))+geom_point();

1.不加Legend

p+theme(legend.position='none');

2.删掉某个legend

方法一:

legend这个数据不要放在aes里面包裹

方法一:

# The base plot (with legend)
p = ggplot(PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot()
# Remove the legend for fill
p + guides(fill=FALSE)

方法二:

 # Remove the legend for fill
 p + scale_fill_discrete(guide=FALSE)
这个fill对应的aes里面的东西,不同的对应有不同得命令

• scale_fill_discrete() 
• scale_fill_hue()
• scale_fill_manual() 
scale_fill_grey()
• scale_fill_brewer()
• scale_colour_discrete()
• scale_colour_hue()
• scale_colour_manual()
• scale_colour_grey() 
• scale_colour_brewer() 
• scale_shape_manual() •
scale_linetype()

方法三:

p + theme(legend.position="none")

2.删除legend.tittle

p+theme(legend.title=element_blank())

3.图例(legend)的位置

图例(legend)的位置和对齐使用的主题设置legend.position来控制,其值可为right,left,top,bottom,none(不加图例,或是一个表示位置的数值。这个数值型位置由legend.justfication给定的相对边角位置表示(取0和1之间的值),它是一个长度为2的数值型向量:右上角为c(1,1),左下角为c(0,0)

例如:

p+theme(legend.position=”left”)

4.legend.tittle的修改

修改legend.title内容

p+scale_colour_hue("what does it eat?",breaks=c("herbi","carni","omni",NA),labels=c("plants","meat","both","don't know"));
如果仅仅修改lengend title,可以:
p+scale_colour_hue("what does it eat?");

方法二:

ggplot(PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot() +guides(fill=guide_legend(title=NULL))

方法三:

scale_fill_hue(guide = guide_legend(title=NULL))

修改legend.title

# The base plot
p = ggplot(PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot()
p
# Set the legend title to "Condition"
p + labs(fill="Condition")

方法二:

p + scale_fill_discrete(name="Condition")

修改legend.text

p+theme(legend.text = element_text(colour = 'red', angle = 45, size = 10, hjust = 3, vjust = 3, face = 'bold'))

改变Legend title的样式

p = ggplot(PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot()
p + theme(legend.title=element_text(face="italic", family="Times", colour="red",size=14))

5.修改尺寸大小

p+theme(legend.background=element_rect(colour="purple",fill="pink",size=3,linetype="dashed"));
p+theme(legend.key.size=unit(2,'cm'));
p+theme(legend.key.width=unit(5,'cm'));

6.颜色的修改以及一致性:

library(RColorBrewer);
newpalette<-colorRampPalette(brewer.pal(12,"Set3"))(length(unique(eee$name)));
p+scale_fill_manual(values=newpalette);

p+geom_bar(position="stack",aes(order=desc(name)))

7.修改legend的text

p = ggplot(PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot()
p
# Change the order of items
p + scale_fill_discrete(limits=c("trt1", "trt2", "ctrl"))

改变顺序

p = ggplot(PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot()
p
# Reverse the legend order
p + guides(fill=guide_legend(reverse=TRUE))

三、常见问题

报错:could not find function “unit”

解决办法:library(grid)

参考资料:

图例变更参考资料:https://github.com/hadley/ggplot2/wiki/Legend-Attributes

http://stackoverflow.com/questions/11714951/remove-extra-legends-in-ggplot2

ggplot2:数据分析与图形艺术

《R Graphics Cookbook》

药企,独角兽,苏州。团队长期招人,感兴趣的都可以发邮件聊聊:tiehan@sina.cn
个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn