p+theme(panel.grid.major.y = element_blank(),panel.grid.minor.y = element_blank())
# 10.图例
# 10.1移除图例
# 使用guides(),并指定需要移除图例的标度
# 基本图形(含图例)
p <-ggplot(PlantGrowth,aes(x=group,y=weight,fill=group))+geom_boxplot()
p
# 移除标度fill的图例
p + guides(fill=FALSE)
# 移除某个图例的另一种方式是在对应标度中设置guide=FALSE.
# 移除标度fill的图例
p+scale_fill_discrete(guide=FALSE)
# 还有一种移除图例的方法是使用主题系统。如果有多于一种带有图例的图形属性映射,这样做将会移除所有图例
p+theme(legend.position = "none")
# 当某个变量被映射到图形属性fill上时,默认使用的标度为scale_fill_discrete()(与scale_fill_hue()等价),这会将不同的因子水平映射到色环上均匀分布的颜色值上。
# 对于fill来说,也有其他的标度可用,如scale_fill_manual()。如果要使用其他图形属性的标度,如colour(针对线和点)或shape(针对点),则必须使用合适的对应标度。
# 10.2 修改图例的位置
# 使用theme(legend.position=...)即可。通过指定位置参数为top、left、right或bottom,图例即可被放置在顶部、左侧、右侧或底部
p <- ggplot(PlantGrowth,aes(x=group,y=weight,fill=group))+geom_boxplot()+scale_fill_brewer(palette = "Pastel2")
p+theme(legend.position = "top")
# 通过指定像legend.position=c(0,1)这样的位置坐标,图例也可被置于绘图区域内部。
# 坐标空间左下角为原点(0,0),右上角为(1,1)。
# 可以使用legend.justification来指定图例框的哪一部分被放置到legend.position所指定的位置上。默认情况下,图例的中心(0.5,0.5)被置于给定的坐标处,但是指定一个不同的点往往是有用的。
# 将图例的右下角(1,0)置于绘图区域的右下角(1,0)
p + theme(legend.position = c(1,0),legend.justification = c(1,0))
# 将图例的右上角置于绘图区域的右上角
p+theme(legend.position = c(1,1),legend.justification = c(1,1))
# 在绘图区域内放置图例时,添加一个不透明的边界使其与图形分开可能会有所帮助
p+theme(legend.position = c(.85,.2))+theme(legend.background = element_rect(fill = "white",colour = "black"))
# 移除图例元素周围的边界以使其融入图形
p+theme(legend.position = c(.85,.2))+theme(legend.background = element_blank())+theme(legend.key = element_blank())
# 10.3修改图例项目的顺序
# 将对应标度的参数limits设置为理想的顺序即可
# 基本图形
p <- ggplot(PlantGrowth,aes(x=group,y=weight,fill=group))+geom_boxplot()
p
# 修改项目顺序
p+scale_fill_discrete(limits=c("trt1","trt2","ctrl"))
# 注意:x轴上项目的顺序并没有改变。要修改这个顺序,需要设置scale_x_discrete()的limits参数,或者修改数据,使其拥有一个不同的因子水平顺序
# 使用灰色调色板
p+scale_fill_grey(start = .5,end = 1,limits=c("trt1","trt2","ctrl"))
# 使用RColorBrewer中的调色板
p+scale_fill_brewer(palette = "Pastel2",limits=c("trt1","trt2","ctrl"))
# 以上都是针对图形属性fill的,要使用其他图形属性的标度,如colour(针对线和点)或shape(针对点),则必须使用合适的对应标度。
# 默认情况下,使用scale_fill_discrete()与使用scale_fill_hue()是等价的。这对于颜色标度也成立。
# 10.4反转图例项目的顺序
# 添加guides(fill=guide_legend(reverse=TRUE))以反转图例的顺序。
# 基本图形
p <- ggplot(PlantGrowth,aes(x=group,y=weight,fill=group))+geom_boxplot()
p
# 反转图例顺序
p+guides(fill=guide_legend(reverse = TRUE))
# 在设定标度的同时也可以控制图例
p+scale_fill_hue(guide=guide_legend(reverse = TRUE))
# 10.5修改图例标题
# 使用函数labs()并设定fill、colour、shape或任何对于图例来说合适的图形属性的值
# 基本图形
p <- ggplot(PlantGrowth,aes(x=group,y=weight,fill=group))+geom_boxplot()
p
# 设置图例标题为“Condition"
p+labs(fill="Condition")
# 在设定标度时也可以设置图例标题。由于图例和坐标轴均为引导元素,这样做与设置x轴或y轴标题的原理是相同的。
p+scale_fill_discrete(name="Condition")
# 如果有多个变量被映射到带有图例的图形属性,可以分别设置每个图例的标题。
library(gcookbook)
# 绘制基本图形
hw <- ggplot(heightweight,aes(x=ageYear,y=heightIn,colour=sex))+geom_point(aes(size=weightLb))+scale_size_continuous(range=c(1,4))
hw
# 使用新的图例标题
hw + labs(colours="Male/Female",size="Weight\n(pounds)")
# 如果有一个变量被分别映射到两个图形属性,则默认会生成一个组合了两种情况的图例。
hw1 <- ggplot(heightweight,aes(x=ageYear,y=heightIn,shape=sex,colour=sex))+geom_point()
hw1
# 要修改图例标题,需要同时设置二者的标题。如果只修改其中一个,则会得到两个分离的图例
# 仅修改shape的标题
hw1+labs(shape="Male/Female")
# 同时修改shape和colour的标题
hw1 +labs(shape="Male/Female",colour="Male/Female")
# 使用函数guides()来控制图例标题
p+guides(fill=guide_legend(title = "Condition"))
# 10.6修改图例标题的外观
# 使用theme(legend.title=element_text())
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))
# 通过guides()来指定图例标题的外观。
p+guides(fill=guide_legend(title.theme = element_text(face = "italic",family = "times",colour = "red",size = 14,angle = 90)))
# 10.7移除图例标题
# 添加语句guides(fill=guide_legend(title=NULL))可以从图例中移除标题。
p+guides(fill=guide_legend(title = NULL))
# 在设置标度的同时也可以控制图例标题。
p+scale_fill_hue(guide=guide_legend(title = NULL))
# 10.8修改图例标签
# 设置标度中的labels参数即可
library(gcookbook)
# 基本图形
p <- ggplot(PlantGrowth,aes(x=group,y=weight,fill=group))+geom_boxplot()
p
# 修改图例标签
p+scale_fill_discrete(labels=c("Control","Treatment 1","Treatment 2"))
# 注意:x轴的标签并没有改变。要修改它,需要设置scale_x_discrete()中的标签,或者修改数据让其拥有不同的因子水平名称
p+scale_fill_grey(start = .5,end = 1,labels=c("Control","Treatment 1","Treatment 2"))
# 如果同时修改了图例项目的顺序,则标签会依照位置顺序与项目进行匹配
p+scale_fill_discrete(limits=c("trt1","trt2","ctrl"),labels=c("Treatment 1","Treatment 2","Control"))
# 如果有一个变量被分别映射到两个图形属性,则默认会生成一个组合了两种情况的图例。
# 如果要修改图例标签,则必须同时修改两种标度中的标签,否则将得到两个分离的图例
# 基本图形
p <- ggplot(heightweight,aes(x=ageYear,y=heightIn,shape=sex,colour=sex))+geom_point()
p
# 修改一个标度中的标签
p+scale_shape_discrete(labels=c("Female","Male"))
# 同时修改两个标度中的标签
p+scale_shape_discrete(labels=c("Female","Male"))+scale_color_discrete(labels=c("Female","Male"))
# 10.9修改图例标签的外观
# 使用theme(legend.text=element_text())
# 基本图形
p <- ggplot(PlantGrowth,aes(x=group,y=weight,fill=group))+geom_boxplot()
# 修改图例标签的外观
p+theme(legend.text = element_text(face = "italic",family = "Times",colour = "red",size = 14))
# 也可以通过guides()来指定图例标签的外观。
# 修改fill对应图例标签文本的外观
p+guides(fill=guide_legend(label.theme = element_text(face = "italic",family = "Times",colour = "red",size = 14,angle = 45)))
# 10.10使用含多行文本的标签
# 在相应标度中设置labels参数,使用\n来表示新行。
p <- ggplot(PlantGrowth,aes(x=group,y=weight,fill=group))+geom_boxplot()
# 含有多于一行文本的标签
# 使用scale_fill_discrete()来控制标度fill的图例
p+scale_fill_discrete(labels=c("Control","Type 1\ntreatment","Type 2\ntreatment"))
# 默认设置下,使用多于一行文本的标签时,各行文本将相互叠加。
# 可以使用theme()增加图例说明的高度并减小各行的间距
# 要实现这个操作,需要使用grid包中的unit()函数来指定高度
library(grid)
p+scale_fill_discrete(labels=c("Control","Type 1\ntreatment","Type 2\ntreatment"))+theme(legend.text = element_text(lineheight = .8),legend.key.height = unit(1,"cm"))