- library(ggplot2)
- bp <- ggplot(data=PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot()
- bp
移除图例
可以使用 guides(fill=FALSE)来移除图例,也可以使用theme改变图中所有的图例。
- # 使用fill
- bp + guides(fill=FALSE)
-
- # 使用scale
- bp + scale_fill_discrete(guide=FALSE)
-
- # 使用theme
- bp + theme(legend.position="none")
下面这行代码会将图例中的顺序改为 trt1,ctrl,trt2:
- bp + scale_fill_discrete(breaks=c("trt1","ctrl","trt2"))
根据指定的颜色,你可能需要使用不同的刻度,比如scale_fill_manual, scale_colour_hue, scale_colour_manual,scale_shape_discrete, scale_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)))
除了使用 scale_fill_discrete,你可能需要使用不同的刻度,比如scale_fill_manual, scale_colour_hue, scale_colour_manual,scale_shape_discrete, scale_linetype_discrete 等等。
移除图例标题我们可以通过下列操作移除图例标题:
- # 移除由fill参数产生的图例标题
- bp + guides(fill=guide_legend(title=NULL))
-
- # 移除所有的图例标题(在这个例子中,其效果与上一行代码相同)
- bp + theme(legend.title=element_blank())
以上内容转自数析学院,关于如何调整图例标题和标签、修改标题和标签的外观、修改图例框、改变图例的位置等内容,可以访问网站学习


雷达卡






京公网安备 11010802022788号







