R语言ggplot2中的theme函数

在这里插入图片描述

大家先来看看这幅图吧,今天想分享的内容就和它相关。本人刚开始用R画图的时候其实比较抵触,因为觉得R画图时有的小细节改起来很麻烦,不如Oringin来的直接。究其原因还是代码不熟悉,学的不够扎实。今天就来重新梳理一下思路,主要内容是ggplot2中的theme函数。

这个theme函数主要的用途是调节图的主题,主题主要分为整幅图的(plot),坐标轴的(axis),图例的(legend),面板的(panel)和分面元素(facet)。其中经常用到的是坐标轴主题的修改,比如常见的坐标轴字体大小的修改。图里每一部分的英文名字大家请看上面那幅图,最好是记下来。

theme函数其实很简单:
theme(主题.部件=element_类型())
主题:plot, axis, legend, panel, facet
部件:title(名字,坐标轴名字), line(线,坐标轴的xy轴), text(标签,坐标轴刻度的数字), ticks(坐标轴刻度的小线条), background(背景)等
类型:rect,line,text
说明:部件要和类型一致。比如,部件为title,text等文字相关的元素,那么类型处就为text。

接下来实际操作一下吧:

library(tidyverse)
head(mtcars)

p1=ggplot(mtcars,aes(mpg,disp))+geom_point()
p2=ggplot(mtcars,aes(mpg,disp,color=cyl))+geom_point()
#1.整幅图的主题设置
p1+labs(title="xyz")+theme(
  plot.background = element_rect(fill = "green", color = "lightgreen", size = 10),
  plot.title = element_text(hjust = 1, color = "red", face = "italic"),
  plot.margin = margin(t = 30, r = 30, b = 30, l = 30, unit = "pt")
)

在这里插入图片描述

#2.坐标轴主题设置
p1+theme(
 axis.line = element_line(color = "green", size = 2),
 axis.title = element_text(color = "grey", face = "italic"),
 axis.ticks = element_line(color = "red", size = 3),
 axis.text = element_text(color = "pink"),
 axis.text.x = element_text(angle = 45, hjust = 1)
)

在这里插入图片描述

#3.面板元素设置
p1+ theme(
 panel.background = element_rect(fill = "pink", color = "blue"),
 panel.grid = element_line(color = "grey80", size = 0.5)
)

在这里插入图片描述

#4.图例设置
p2+theme(
 legend.background = element_rect(fill = "grey"),
 legend.title = element_text(color = "green", size = 10),
 legend.key = element_rect(fill = "black"),
 legend.text = element_text(color = "red"),
 legend.margin = margin(t = 20, r = 10, b = 10, l = 10, unit = "pt"),
 legend.position = "top"
)

在这里插入图片描述

R语言的数据可视化功能是非常强大的,现在的我早以抛弃Origin了,当有学生介绍科研绘图软件还为Oringin和sigmaplot时,我很想建议他该从“所见既所得”阶段跨到“所想既所得”阶段了。

好了,今天的分享就到这吧~
PS:大家对森林生态学和R感兴趣的话,请关注我的个人微信公众号哟(森林生态小小圈)~

参考资料:《数据科学中的R语言》——王敏杰

  • 30
    点赞
  • 139
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
使用ggplot2绘制正弦和余弦函数图像可以按照以下步骤进行: 1. 首先,我们需要创建一个数据框来存储正弦和余弦函数的数据点,可以使用seq函数生成一组x值,然后用sin和cos函数计算相应的y值: ``` x <- seq(0, 2*pi, length.out = 100) data <- data.frame(x = x, sin = sin(x), cos = cos(x)) ``` 2. 接下来,使用ggplot函数创建一个基本的图层,设置x轴和y轴范围、标签等: ``` library(ggplot2) ggplot(data, aes(x)) + xlim(0, 2*pi) + ylim(-1, 1) + xlab("x") + ylab("y") ``` 3. 在这个基本图层上,使用geom_line函数添加正弦函数和余弦函数的线条: ``` ggplot(data, aes(x)) + xlim(0, 2*pi) + ylim(-1, 1) + xlab("x") + ylab("y") + geom_line(aes(y = sin), color = "red") + geom_line(aes(y = cos), color = "blue") ``` 4. 最后,可以使用其他ggplot函数来调整图像的颜色、线条样式、标题等等,生成最终的图像: ``` ggplot(data, aes(x)) + xlim(0, 2*pi) + ylim(-1, 1) + xlab("x") + ylab("y") + geom_line(aes(y = sin), color = "red") + geom_line(aes(y = cos), color = "blue") + theme_bw() + ggtitle("Sin and Cos Functions") ``` 完整的代码如下: ``` x <- seq(0, 2*pi, length.out = 100) data <- data.frame(x = x, sin = sin(x), cos = cos(x)) ggplot(data, aes(x)) + xlim(0, 2*pi) + ylim(-1, 1) + xlab("x") + ylab("y") + geom_line(aes(y = sin), color = "red") + geom_line(aes(y = cos), color = "blue") + theme_bw() + ggtitle("Sin and Cos Functions") ``` 生成的图像如下: ![Sin and Cos Functions](https://i.imgur.com/5l9LsKx.png)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值