R語言ggplot2圖例修改超詳細介紹
前言
大家經(jīng)常對ggplot2
中的圖例不滿意,想要各種修改,今天就介紹下圖例的各種修改細節(jié),基本上常用的操作都涉及到了!
library(ggplot2) library(gcookbook)
移除圖例
提供3種方法可以在ggplot2
中移除圖例。
# 基本圖形 pg_plot <- ggplot(PlantGrowth, aes(x = group, y = weight, fill = group)) + geom_boxplot() pg_plot
# 首先可以使用guides()函數(shù)移除圖例 pg_plot + guides(fill = "none")
然后可以在scale_**
函數(shù)中移除,這里是fill,你要根據(jù)自己的情況換成shape、color等。
pg_plot+scale_fill_discrete(guide = "none")
第3種方法是在theme中移除。
pg_plot+theme(legend.position = "none")
改變圖例位置
也是在theme
中更改,
pg_plot <- ggplot(PlantGrowth, aes(x = group, y = weight, fill = group)) + geom_boxplot() + scale_fill_brewer(palette = "Pastel2") pg_plot + theme(legend.position = "top") # 放在頂部
legend.position的參數(shù)可以是left、right、top、bottom,還可以是坐標。
pg_plot+theme(legend.position = c(0.8,0.3))
在ggplot2
中,左下角的坐標是c(0,0),右上角的坐標是c(1,1),你可以自己設(shè)置想要放置的位置。需要注意的是,你設(shè)置的這個坐標是圖例中心點的坐標,可以通過legend.justification
設(shè)置圖例的哪個位置放在你的坐標上。
# 圖例右下角,放在畫布右下角 pg_plot + theme(legend.position = c(1, 0), legend.justification = c(1, 0))
p <- ggplot(heightweight, aes(x=ageYear, y=heightIn, shape=sex, colour=sex))+geom_point() p
兩個圖例更改為水平排列:
p + scale_shape_discrete(label = c("female","male")) + theme(legend.direction = "horizontal")
修改圖例的邊框和背景
pg_plot+ theme(legend.position = c(0.85,0.2))+ theme(legend.background = element_rect(fill = "orange",color = "black"))
中間還有一部分是白色,需要另外一個參數(shù)修改:
pg_plot+ theme(legend.position = c(0.85,0.2))+ theme(legend.background = element_rect(fill = "orange",color = "black"), legend.key = element_rect(fill = "orange") )
改變圖例順序
pg_plot <- ggplot(PlantGrowth, aes(x = group, y = weight, fill = group)) + geom_boxplot() pg_plot
# limits pg_plot + scale_fill_discrete(limits = c("trt1", "trt2", "ctrl"))
也可以在畫圖之前,通過因子化的方法把數(shù)據(jù)先排好序再畫圖哦。
反轉(zhuǎn)圖例順序
2種方法。
pg_plot+scale_fill_discrete(guide = guide_legend(reverse = T))
pg_plot+guides(fill = guide_legend(reverse = T))
修改圖例標題
2種方法。
pg_plot <- ggplot(PlantGrowth, aes(x = group, y = weight, fill = group)) + geom_boxplot() pg_plot
labs
里面修改
pg_plot + labs(fill = "Condition")
在scale_*
函數(shù)里面修改。
pg_plot+scale_fill_discrete(name = "hahah")
還有一種比較復雜的情況,如果多個一個變量映射給多個圖形參數(shù),或者有多個圖例,怎么修改呢?像下面這個例子,sex
和shape、color都有關(guān)。
p <- ggplot(heightweight, aes(x=ageYear, y=heightIn, shape=sex, colour=sex))+geom_point() p
可以在scale_*
函數(shù)中修改:
p + scale_shape_discrete(name = "shape")+ scale_color_discrete(name = "colooorrr")
也可以在legend中修改:
p + labs(shape = "shapppeee",color = "colooorrr")
還可以在guides函數(shù)中修改:
p + guides(shape = guide_legend(title = "this is\nshape"), color = guide_legend(title = "cooolor") )
修改圖例標題外觀
pg_plot <- ggplot(PlantGrowth, aes(x = group, y = weight, fill = group)) + geom_boxplot() pg_plot
可以在theme中修改:
pg_plot + theme( legend.title = element_text( face = "italic", family = "Times", colour = "red", size = 18 ) )
也可以在guides中修改:
pg_plot + guides(fill = guide_legend(title.theme = element_text( face = "italic", family = "Times", colour = "red", size = 14)) )
移除圖例標題
pg_plot
可以在theme中修改,也可以在scale_xxx函數(shù)中修改,也可以在guides函數(shù)中修改。
pg_plot+theme(legend.title = element_blank())
pg_plot+scale_fill_discrete(name = NULL)
pg_plot+scale_fill_hue(guide = guide_legend(title = NULL))
pg_plot+guides(fill = guide_legend(title = NULL))
修改圖例標簽
pg_plot
pg_plot+scale_fill_discrete(labels = c("label1","label2","label3"))
修改圖例標簽外觀
在theme中修改:
pg_plot + theme(legend.text = element_text( colour = "red", face = "italic", size = 22) )
總結(jié)
到此這篇關(guān)于R語言ggplot2圖例修改的文章就介紹到這了,更多相關(guān)R語言ggplot2圖例修改內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- R語言中g(shù)gplot2繪制雙坐標軸圖
- R語言ggplot2設(shè)置圖例(legend)的操作大全
- R語言ggplot2圖例標簽、標題、順序修改和刪除操作實例
- R語言ggplot2拼圖包patchwork安裝使用
- R語言包ggplot實現(xiàn)分面去掉小標題的灰色底色小技巧
- R語言學習ggplot2繪制統(tǒng)計圖形包全面詳解
- R語言數(shù)據(jù)可視化ggplot繪制置信區(qū)間與分組繪圖技巧
- R語言使用ggplot繪制畫中畫細節(jié)放大的方法
- R語言ggplot2之圖例的設(shè)置
- R語言ggplot2包之坐標軸詳解
- R語言ggplot在熱圖上標注相關(guān)系數(shù)的操作方法
相關(guān)文章
R語言如何將大型Excel文件轉(zhuǎn)為dta格式詳解
這篇文章主要給大家介紹了關(guān)于R語言如何將大型Excel文件轉(zhuǎn)為dta格式的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03使用R語言繪制3D數(shù)據(jù)可視化scatter散點圖實現(xiàn)步驟
這篇文章主要為大家介紹了使用R語言繪制3D數(shù)據(jù)可視化scatter散點圖的實現(xiàn)步驟,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-02-02R語言-如何將科學計數(shù)法表示的數(shù)字轉(zhuǎn)化為文本
這篇文章主要介紹了R語言-如何將科學計數(shù)法表示的數(shù)字轉(zhuǎn)化為文本,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04R語言實現(xiàn)操作MySQL數(shù)據(jù)庫
這篇文章主要介紹了R語言實現(xiàn)操作MySQL數(shù)據(jù)庫,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03R語言編程學習從Github上安裝包解決網(wǎng)絡(luò)問題
這篇文章主要為大家介紹了R語言編程從Github上安裝包的過程詳解,這樣可以解決很多網(wǎng)絡(luò)問題,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11基于R/RStudio中安裝包“無法與服務(wù)器建立連接”的解決方案
這篇文章主要介紹了基于R/RStudio中安裝包“無法與服務(wù)器建立連接”的解決方案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04