ggplot2-设置图例(legend)

本文更新地址:http://blog.csdn.net/tanzuozhev/article/details/51108040

本文在 http://www.cookbook-r.com/Graphs/Scatterplots_(ggplot2)/ 的基础上加入了自己的理解

图例用来解释图中的各种含义,比如颜色,形状,大小等等, 在ggplot2中aes中的参数(x, y 除外)基本都会生成图例来解释图形, 比如 fill, colour, linetype, shape.

基本箱线图(带有图例)

library(ggplot2)
bp <- ggplot(data=PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot()
bp

移除图例

Use guides(fill=FALSE), replacing fill with the desired aesthetic. 使用 guides(fill=FALSE) 移除由ase中 匹配的fill生成的图例, 也可以使用theme You can also remove all the legends in a graph, using theme.

bp + guides(fill=FALSE)

# 也可以这也
bp + scale_fill_discrete(guide=FALSE)

# 移除所有图例
bp + theme(legend.position="none")

修改图例的内容

改变图例的顺序为 trt1, ctrl, trt2:

bp + scale_fill_discrete(breaks=c("trt1","ctrl","trt2"))

 根据不同的分类,可以使用 scale_fill_manualscale_colour_hue,scale_colour_manualscale_shape_discretescale_linetype_discrete 等等.

颠倒图例的顺序

# 多种方法
bp + guides(fill = guide_legend(reverse=TRUE))

# 也可以
bp + scale_fill_discrete(guide = guide_legend(reverse=TRUE))

# 还可以这也
bp + scale_fill_discrete(breaks = rev(levels(PlantGrowth$group)))

隐藏图例标题

# Remove title for fill legend
bp + guides(fill=guide_legend(title=NULL))

# Remove title for all legends
bp + theme(legend.title=element_blank())

修改图例中的标签

两种方法一种是直接修改标签, 另一种是修改data.frame

Using scales

图例可以根据 fill, colour, linetype, shape 等绘制, 我们以 fill 为例, scale_fill_xxxxxx 表示处理数据的一种方法, 可以是 hue(对颜色的定量操作), continuous(连续型数据处理), discete(离散型数据处理)等等.

# 设置图例名称
bp + scale_fill_discrete(name="Experimental\nCondition")

# 设置图例的名称, 重新定义新的标签名称
bp + scale_fill_discrete(name="Experimental\nCondition",
                         breaks=c("ctrl", "trt1", "trt2"),
                         labels=c("Control", "Treatment 1", "Treatment 2"))

# 自定义fill的颜色
bp + scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"), 
                       name="Experimental\nCondition",
                       breaks=c("ctrl", "trt1", "trt2"),
                       labels=c("Control", "Treatment 1", "Treatment 2"))

注意这里并不能修改 x轴 的标签,如果需要改变x轴的标签,可以参照http://blog.csdn.net/tanzuozhev/article/details/51107583

# A different data set
df1 <- data.frame(
    sex = factor(c("Female","Female","Male","Male")),
    time = factor(c("Lunch","Dinner","Lunch","Dinner"), levels=c("Lunch","Dinner")),
    total_bill = c(13.53, 16.81, 16.24, 17.42)
)

# A basic graph
lp <- ggplot(data=df1, aes(x=time, y=total_bill, group=sex, shape=sex)) + geom_line() + geom_point()
lp

# 修改图例
lp + scale_shape_discrete(name  ="Payer",
                          breaks=c("Female", "Male"),
                          labels=c("Woman", "Man"))

 If you use both colour and shape, they both need to be given scale specifications. Otherwise there will be two two separate legends. 如果同时使用 colorshape,那么必须都进行scale_xx_xxx的定义,否则colorshape的图例就会合并到一起, 如果 scale_xx_xxx 中的name相同,那么他们也会合并到一起.

# Specify colour and shape
lp1 <- ggplot(data=df1, aes(x=time, y=total_bill, group=sex, shape=sex, colour=sex)) + geom_line() + geom_point()
lp1

# Here's what happens if you just specify colour
lp1 + scale_colour_discrete(name  ="Payer",
                            breaks=c("Female", "Male"),
                            labels=c("Woman", "Man"))

你可能感兴趣的:(R语言,R语言与数据可视化)