日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

R語言ggplot2圖例標簽、標題、順序修改和刪除操作實例_R語言

作者:小Li先生 ? 更新時間: 2022-09-14 編程語言

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"))

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")

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())

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

5. 刪除圖例

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

引用

  • R語言之可視化①⑨子ggplot2中的圖例修改
  • ggplot2-設置圖例(legend)

總結

原文鏈接:https://blog.csdn.net/LeaningR/article/details/114576555

欄目分類
最近更新