ZT:改動(dòng)ggplot2中的某些標(biāo)簽

original address:https://www.datanovia.com/en/blog/how-to-change-ggplot-labels/

library(ggplot2) 
theme_set(theme_classic())
#Create a basic plot using the dataset ToothGrowth:

# Convert the variable dose from numeric to factor variable
ToothGrowth$dose <- as.factor(ToothGrowth$dose)

# Create a boxplot colored by dose group levels
bxp <- ggplot(ToothGrowth, aes(x = dose, y = len)) + 
  geom_boxplot(aes(color = dose)) +
  scale_color_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))
bxp

Key R functions

labs(..., title = waiver(), subtitle = waiver(), caption = waiver(),
  tag = waiver())

xlab(label)

ylab(label)

ggtitle(label, subtitle = waiver())

A list of new name-value pairs. The name should be an aesthetic. For example p + labs(title = "Main title", x = "X axis label", y = "Y axis label") changes main title and axis labels.
title: plot main title.
subtitle: the text for the subtitle for the plot which will be displayed below the title.
caption: the text for the caption which will be displayed in the bottom-right of the plot by default.
tag: the text for the tag label which will be displayed at the top-left of the plot by default.
label: the title of the respective axis (for xlab() or ylab()) or of the plot (for ggtitle()).

Add titles and axis labels

In this section, we’ll use the function labs() to change the main title, the subtitle, the axis labels and captions.

It’s also possible to use the functions ggtitle(), xlab() and ylab() to modify the plot title, subtitle, x and y axis labels.

Add a title, subtitle, caption and change axis labels:

bxp <- bxp + labs(title = "Effect of Vitamin C on Tooth Growth",
              subtitle = "Plot of length by dose",
              caption = "Data source: ToothGrowth",
              x = "Dose (mg)", y = "Teeth length",
              tag = "A")
bxp

Modify legend titles

You can use labs() to changes the legend title for a given aesthetics (fill, color, size, shape, . . . ). For example:

Use p + labs(fill = "dose") for geom_boxplot(aes(fill = dose))
Use p + labs(color = "dose") for geom_boxplot(aes(color = dose))
and so on for linetype, shape, etc

bxp + labs(color = "Dose (mg)")

Split long titles

If the title is too long, you can split it into multiple lines using \n. In this case you can adjust the space between text lines by specifying the argument lineheight in the theme function element_text():

bxp + labs(title = "Effect of Vitamin C on Tooth Growth \n in Guinea Pigs")+
  theme(plot.title = element_text(lineheight = 0.9))
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • A ggplot2 Tutorial for Beautiful Plotting in R 原文見(jiàn)https:/...
    iColors閱讀 2,925評(píng)論 0 15
  • 作者:嚴(yán)濤浙江大學(xué)作物遺傳育種在讀研究生(生物信息學(xué)方向)偽碼農(nóng),R語(yǔ)言愛(ài)好者,愛(ài)開(kāi)源 ggplot2學(xué)習(xí)筆記之圖...
    Dylan的迪閱讀 2,865評(píng)論 0 6
  • 寫(xiě)在前面 ggplot2 是一個(gè)功能強(qiáng)大且靈活的R包 ,由Hadley Wickham 編寫(xiě),其用于生成優(yōu)雅的圖...
    Boer223閱讀 28,568評(píng)論 0 67
  • 簡(jiǎn)介 文章較長(zhǎng),點(diǎn)擊直達(dá)我的博客,瀏覽效果更好。本文內(nèi)容基本是來(lái)源于STHDA,這是一份十分詳細(xì)的ggplot2使...
    taoyan閱讀 51,712評(píng)論 7 159
  • 久違的晴天,家長(zhǎng)會(huì)。 家長(zhǎng)大會(huì)開(kāi)好到教室時(shí),離放學(xué)已經(jīng)沒(méi)多少時(shí)間了。班主任說(shuō)已經(jīng)安排了三個(gè)家長(zhǎng)分享經(jīng)驗(yàn)。 放學(xué)鈴聲...
    飄雪兒5閱讀 7,819評(píng)論 16 22

友情鏈接更多精彩內(nèi)容