R語(yǔ)言ggplot2?title設(shè)置教程(main,axis和legend?titles)
1. ggplot2中添加title函數(shù)
ggtitle(label) # for the main title,主題目 xlab(label) # for the x axis label, xlab ylab(label) # for the y axis label, ylab labs(...) # for the main title, axis labels and legend titles,可以同時(shí)設(shè)定多個(gè)lab和tittle
2. 實(shí)際應(yīng)用
(1)添加title、xlab和ylab
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
library(ggplot2)
p <- ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot()
## 方法1:
p + ggtitle("Plot of length \n by dose") +
xlab("Dose (mg)") + ylab("Teeth length")
## 方法2:
p +labs(title="Plot of length \n by dose",
x ="Dose (mg)", y = "Teeth length")
(2)修改legend名字
# Default plot p <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose))+ geom_boxplot() p # Modify legend titles p + labs(fill = "Dose (mg)")
(3)修改title的字體,位置,顏色等
參數(shù),其中hjust和vjust可以調(diào)節(jié)位置, anglexlab和ylab調(diào)節(jié)角度,size可以調(diào)節(jié)label大?。?/p>
family : font family face : font face. Possible values are “plain”, “italic”, “bold” and “bold.italic” colour : text color size : text size in pts hjust : horizontal justification (in [0, 1]) vjust : vertical justification (in [0, 1]) lineheight : line height. In multi-line text, the lineheight argument is used to change the spacing between lines. color : an alias for colour angle: angle
使用:
# Default plot
p <- ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot() +
ggtitle("Plot of length \n by dose") +
xlab("Dose (mg)") + ylab("Teeth length")
p
# Change the color, the size and the face of
# the main title, x and y axis labels
p + theme(
plot.title = element_text(color="red", size=14, face="bold.italic"),
axis.title.x = element_text(color="blue", size=14, face="bold"),
axis.title.y = element_text(color="#993333", size=14, face="bold")
)
此外,修改坐標(biāo)軸的angle也是相似的構(gòu)造:
require(ggplot2) ggplot(data=mtcars, aes(x=mpg, y=wt)) + geom_point() + theme(axis.text.x = element_text(angle=90)) ggplot(data=mtcars, aes(x=mpg, y=wt)) + geom_point() + theme(axis.text.y = element_text(angle=90))
(4)刪除xlab和ylab
# Hide the main title and axis titles p + theme( plot.title = element_blank(), axis.title.x = element_blank(), axis.title.y = element_blank())
總之,一次性設(shè)定ggplot相關(guān)title的話, labs(title=" ", x=" ",y=" ")即可,修改需要使用后面的theme(axis.text.x = element_text(angle=90)),類似這種設(shè)定。
翻譯來源:
http://www.sthda.com/english/wiki/ggplot2-title-main-axis-and-legend-titles
總結(jié)
到此這篇關(guān)于R語(yǔ)言ggplot2 title設(shè)置教程(main,axis和legend titles)的文章就介紹到這了,更多相關(guān)R語(yǔ)言ggplot2 title設(shè)置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- R語(yǔ)言ggplot2設(shè)置圖例(legend)的操作大全
- R語(yǔ)言ggplot2之圖例的設(shè)置
- R語(yǔ)言 ggplot2改變柱狀圖的順序操作
- R語(yǔ)言ggplot2包之坐標(biāo)軸詳解
- R語(yǔ)言 使用ggplot2繪制好看的分組散點(diǎn)圖
- R語(yǔ)言ggplot2邊框背景去除的實(shí)現(xiàn)
- R語(yǔ)言學(xué)習(xí)ggplot2繪制統(tǒng)計(jì)圖形包全面詳解
- R語(yǔ)言 解決安裝ggplot2報(bào)錯(cuò)的問題
- R語(yǔ)言ggplot2x軸順序設(shè)置自定義顏色的操作
- R語(yǔ)言ggplot2圖例修改超詳細(xì)介紹
相關(guān)文章
R語(yǔ)言數(shù)據(jù)讀取以及數(shù)據(jù)保存方式
這篇文章主要介紹了R語(yǔ)言數(shù)據(jù)讀取以及數(shù)據(jù)保存方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-04-04
R語(yǔ)言數(shù)據(jù)可視化繪圖bar chart條形圖實(shí)現(xiàn)示例
這篇文章主要為大家介紹了R語(yǔ)言數(shù)據(jù)可視化繪圖bar chart條形圖的實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02
R語(yǔ)言入門使用RStudio制作包含Rcpp代碼的R包
這篇文章主要為大家介紹了R語(yǔ)言入門使用RStudio來制作包含Rcpp代碼的R包,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11
R語(yǔ)言表達(dá)式知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理了一篇關(guān)于R語(yǔ)言表達(dá)式知識(shí)點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。2021-03-03
R語(yǔ)言中dnorm,pnorm,qnorm和rnorm的區(qū)別淺析
正在學(xué)習(xí)R語(yǔ)言統(tǒng)計(jì)學(xué)的小伙伴,可能會(huì)被各種專有名詞所困擾,下面這篇文章主要給大家介紹了關(guān)于R語(yǔ)言中dnorm,pnorm,qnorm和rnorm區(qū)別的相關(guān)資料,需要的朋友可以參考下2022-12-12
R語(yǔ)言科學(xué)計(jì)數(shù)法介紹:digits和scipen設(shè)置方式
這篇文章主要介紹了R語(yǔ)言科學(xué)計(jì)數(shù)法介紹:digits和scipen設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-04-04
R語(yǔ)言學(xué)習(xí)筆記缺失數(shù)據(jù)的Bootstrap與Jackknife方法
這篇文章主要為大家介紹了R語(yǔ)言學(xué)習(xí)筆記關(guān)于缺失數(shù)據(jù)的Bootstrap與Jackknife的方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11

