广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >R语言ggplot2 title设置教程(main,axis和legend titles)
  • 376
分享到

R语言ggplot2 title设置教程(main,axis和legend titles)

r语言ggplot2 titler语言ggplot绘图教程r语言中ggplot2 2023-02-13 12:02:28 376人浏览 八月长安
摘要

目录1. ggplot2中添加title函数2. 实际应用(1)添加title、xlab和ylab(2)修改legend名字(3)修改title的字体,位置,颜色等(4)删除xlab

1. ggplot2中添加title函数

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,可以同时设定多个lab和tittle

2. 实际应用

(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的字体,位置,颜色等

参数,其中hjust和vjust可以调节位置, anglexlab和ylab调节角度,size可以调节label大小:

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")
)

此外,修改坐标轴的angle也是相似的构造:

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())

总之,一次性设定ggplot相关title的话, labs(title=" ", x=" ",y=" ")即可,修改需要使用后面的theme(axis.text.x = element_text(angle=90)),类似这种设定。

翻译来源:

Http://www.sthda.com/english/wiki/ggplot2-title-main-axis-and-legend-titles

总结

到此这篇关于R语言ggplot2 title设置教程(main,axis和legend titles)的文章就介绍到这了,更多相关R语言ggplot2 title设置内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: R语言ggplot2 title设置教程(main,axis和legend titles)

本文链接: https://www.lsjlt.com/news/195798.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作