ZT:改动ggplot2中的某些标签

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))
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • A ggplot2 Tutorial for Beautiful Plotting in R 原文见https:/...
    iColors阅读 2,825评论 0 15
  • 作者:严涛浙江大学作物遗传育种在读研究生(生物信息学方向)伪码农,R语言爱好者,爱开源 ggplot2学习笔记之图...
    Dylan的迪阅读 2,679评论 0 6
  • 写在前面 ggplot2 是一个功能强大且灵活的R包 ,由Hadley Wickham 编写,其用于生成优雅的图...
    Boer223阅读 28,262评论 0 67
  • 简介 文章较长,点击直达我的博客,浏览效果更好。本文内容基本是来源于STHDA,这是一份十分详细的ggplot2使...
    taoyan阅读 51,414评论 7 159
  • 久违的晴天,家长会。 家长大会开好到教室时,离放学已经没多少时间了。班主任说已经安排了三个家长分享经验。 放学铃声...
    飘雪儿5阅读 7,562评论 16 22