R语言ggplot2图例标签、标题、顺序修改和删除操作实例

来自:网络
时间:2023-05-18
阅读:
目录

1. 修改图例标注/标签

内容修改、序列修改

##标注排序修改breaks,breaks——labels,
p + scale_colour_discrete(#values=c("#CC0000", "#006600", "#669999",
# "#00CCCC", "#660099"),或者color/fill/colour,discrete/hue,
    name="Experimental",
    breaks = c("6","4","8"),
    labels = c("B","A","C"))

R语言ggplot2图例标签、标题、顺序修改和删除操作实例

2. 修改图例标题名称

##第一种color/fill/colour,discrete/hue
p + scale_colour_discrete(
    name="Experimental",
    breaks = c("6","4","8"),
    labels = c("B","A","C"))
##第二种修改图例标题
p + labs(colour = "name1", shape = "name2")

R语言ggplot2图例标签、标题、顺序修改和删除操作实例

3. 隐藏/删除图例标题 标题修改、删除

#第一种,
#Remove title for fill legend
p + guides(colour=guide_legend(title=NULL)) #注意colour/fill/color/shape转换
#第二种,所有的表题全部移除
# Remove title for all legends
p + theme(legend.title=element_blank())

R语言ggplot2图例标签、标题、顺序修改和删除操作实例

4.图例位置

#1: around the plot "none", "left", "right", "bottom", "top"
basic + theme(legend.position = "bottom")
#2:inside the plot
basic + theme(
    legend.position = c(.95, .95),#plot内位置
    legend.justification = c("right", "top"),#固定右上角
    legend.background = element_blank(),#图例背景色
    legend.key = element_blank(),#图标背景色element_rect(colour="black")
    legend.box.background = element_rect(fill=NA,color = "black",linetype = 1) #图例外框和背景色默认填充白色(删除fill=NA)
    #legend.box.just = "right",
    #legend.margin = margin(6, 6, 6, 6)#边框大小调整
    )

图片呈现是的3

R语言ggplot2图例标签、标题、顺序修改和删除操作实例

5. 删除图例

#删除其中一个图例
guides(shape=FALSE)   #shape/color/colour/fill
#全部删除图例
theme(legend.position = "none")

R语言ggplot2图例标签、标题、顺序修改和删除操作实例

引用

总结

返回顶部
顶部