R語言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è)定多個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é)位置, angle
xlab和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語言ggplot2 title設(shè)置教程(main,axis和legend titles)的文章就介紹到這了,更多相關(guān)R語言ggplot2 title設(shè)置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
R語言實(shí)現(xiàn)操作MySQL數(shù)據(jù)庫
這篇文章主要介紹了R語言實(shí)現(xiàn)操作MySQL數(shù)據(jù)庫,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03R語言實(shí)現(xiàn)ggplot重繪天貓雙十一銷售額曲線圖過程
這篇文章主要為大家介紹了如何使用ggplot繪制天貓雙十一銷售額曲線圖的實(shí)現(xiàn)過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2021-11-11