ggplot2会自动生成图例,我们可以对图例进行删改。
相同名称相同颜色的图例会自动合并。如下图,点图和线图的color、shape产生的图例都是相同名称相同颜色的,所以合并在一次:
library(ggplot2)
dt = data.frame(A = 1:10, B = c(2,21,6,18,9,25,13,15,10,3), C = c(‘A‘,‘C‘,‘A‘,‘B‘,‘C‘,‘D‘,‘A‘,‘C‘,‘D‘,‘B‘))
p = ggplot(dt, aes(x = A, y = B, group = factor(1))) +
geom_point(aes(color = C, shape = C), size = 3.8) +
geom_line(aes(color = C), size = 0.8)
p
方法一:用 guides(fill=FALSE) 隐藏由fill产生的图例,同理可以隐藏由color、shape产生的图例。
方法二:增加图层的时候把参数 show_guide 设为 FALSE,隐藏某图层的图例。
方法三:作用和方法一类似,设置 scale_fill_discrete(guide = FALSE),可以把中间的 fill 改成 color 或者 shape,从而隐藏相应的图例。
方法三:用 theme(legend.position = “none”) 来隐藏全局图例。
library(ggplot2)
dt = data.frame(A = 1:10, B = c(2,21,6,18,9,25,13,15,10,3), C = c(‘A‘,‘C‘,‘A‘,‘B‘,‘C‘,‘D‘,‘A‘,‘C‘,‘D‘,‘B‘))
p = ggplot(dt, aes(x = A, y = B, group = factor(1))) +
geom_point(aes(color = C, shape = C), size = 3.8) +
geom_line(aes(color = C), size = 0.8, show_guide = FALSE) ##隐藏此图层产生的图例
# + guides(fill=FALSE) ##隐藏由fill产生的图例
# + scale_fill_discrete(guide = FALSE) ##隐藏由fill产生的图例
# + theme(legend.position = "none") ##隐藏所有图例
p
library(ggplot2)
dt = data.frame(A = 1:10, B = c(2,21,6,18,9,25,13,15,10,3), C = c(‘A‘,‘C‘,‘A‘,‘B‘,‘C‘,‘D‘,‘A‘,‘C‘,‘D‘,‘B‘))
p = ggplot(dt, aes(x = A, y = B, group = factor(1))) +
geom_point(aes(color = C, shape = C), size = 3.8) +
geom_line(aes(color = C), size = 0.8)
p + guides(color=guide_legend(title=NULL)) ##对color产生的图例去掉标题
# p + theme(legend.title=element_blank()) ##把所有图例的标题去掉
用 scale_color_discrete,通过 breaks 参数设置图例的顺序(注意也可以把color改成fill或者shape):
library(ggplot2)
dt = data.frame(A = 1:10, B = c(2,21,6,18,9,25,13,15,10,3), C = c(‘A‘,‘C‘,‘A‘,‘B‘,‘C‘,‘D‘,‘A‘,‘C‘,‘D‘,‘B‘))
p = ggplot(dt, aes(x = A, y = B, group = factor(1))) +
geom_point(aes(color = C, shape = C), size = 3.8) +
geom_line(aes(color = C), size = 0.8) +
scale_color_discrete(breaks = c(‘B‘,‘C‘,‘A‘,‘D‘))
p
直接将图例逆序:
p + guides(color = guide_legend(reverse=TRUE))
或者:p + scale_color_discrete(guide = guide_legend(reverse=TRUE))
或者:p + scale_color_discrete(breaks = rev(levels(dt$C)))
library(ggplot2)
dt = data.frame(A = 1:10, B = c(2,21,6,18,9,25,13,15,10,3), C = c(‘A‘,‘C‘,‘A‘,‘B‘,‘C‘,‘D‘,‘A‘,‘C‘,‘D‘,‘B‘))
p = ggplot(dt, aes(x = A, y = B, group = factor(1))) +
geom_point(aes(color = C, shape = C), size = 3.8) +
geom_line(aes(color = C), size = 0.8) +
scale_colour_discrete(breaks = c(‘A‘,‘B‘,‘C‘,‘D‘), labels = c(‘W‘,‘X‘,‘Y‘,‘Z‘))
p
library(ggplot2)
dt = data.frame(A = 1:10, B = c(2,21,6,18,9,25,13,15,10,3), C = c(‘A‘,‘C‘,‘A‘,‘B‘,‘C‘,‘D‘,‘A‘,‘C‘,‘D‘,‘B‘))
p = ggplot(dt, aes(x = A, y = B, group = factor(1))) +
geom_point(aes(color = C, shape = C), size = 3.8) +
geom_line(aes(color = C), size = 0.8) +
theme(legend.title = element_text(color="134", size=16, face="bold")) + ##设置标题,face="bold"加粗
theme(legend.text = element_text(color="azure4", size = 14, face = "bold")) ##设置标签文字
p
library(ggplot2)
dt = data.frame(A = 1:10, B = c(2,21,6,18,9,25,13,15,10,3), C = c(‘A‘,‘C‘,‘A‘,‘B‘,‘C‘,‘D‘,‘A‘,‘C‘,‘D‘,‘B‘))
p = ggplot(dt, aes(x = A, y = B, group = factor(1))) +
geom_point(aes(color = C, shape = C), size = 3.8) +
geom_line(aes(color = C), size = 0.8) +
theme(legend.position = ‘top‘) ##同理可以取 bottom、right、left
p
更多图例信息请参考:Legends (ggplot2)
版权声明:转载请注明出处,谢谢!
原文地址:http://blog.csdn.net/bone_ace/article/details/47284805