R语言可视化——ggplot2包的八种默认主题及其扩展包

0引言

R语言中的ggplot2包里面的风格固定,在需要特殊的图形时,需要更改甚至自定义设置主题。本文展示ggplot2包里的八种默认的主题,并介绍扩展包ggthemes去自定义主题内容。

八种主题函数汇总表

主题函数效果
theme_bw()网格白色主题
theme_classic()经典主题
theme_dark()暗色主题,可用于对比
theme_gray()默认主题
theme_light()浅色坐标带网格
theme_linedraw()黑色网格线
theme_minimal()极简主题
theme_void()空白主题

主题汇总图

代码:

p1 <- ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, col = class, shape = class)) + 
theme_bw() + 
labs(title = "网格白色主题")+  theme(legend.position = "none")
p2 <- ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, col = class, shape = class)) + 
theme_classic() + 
labs(title = "经典主题")+  theme(legend.position = "none")
p3 <- ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, col = class, shape = class)) + 
theme_dark() + 
labs(title = "暗色主题,可用于对比")+  theme(legend.position = "none")
p4 <- ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, col = class, shape = class)) + 
theme_gray() + 
labs(title = "默认主题")+  theme(legend.position = "none")
p5 <- ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, col = class, shape = class)) + 
theme_light() + 
labs(title = "浅色坐标带网格")+  theme(legend.position = "none")
p6 <- ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, col = class, shape = class)) + 
theme_linedraw() + 
labs(title = "黑色网格线")+  theme(legend.position = "none")
p7 <- ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, col = class, shape = class)) + 
theme_minimal() + 
labs(title = "极简主题")+  theme(legend.position = "none")
p8 <- ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, col = class, shape = class)) + 
theme_void()  + 
labs(title = "空白主题")+  theme(legend.position = "none")
grid.arrange(p1,p2,p3,p4,p5,p6,p7,p8, nrow=2)

在这里插入图片描述

扩展包

如果感觉上面的主题话没有你满意的可以去加载ggthemes包,里面会有更加个性化的参数,创作出你想要的主题。

以下是将Excel中的数据进行单因素方差分析并使用ggplot2可视化R语言代码: ```R # 导入必要的库 library(readxl) library(dplyr) library(stats) library(ggplot2) # 读取Excel数据 data <- read_excel("data.xlsx") # 单因素方差分析 fit <- aov(value ~ group, data = data) # 显示ANOVA结果 summary(fit) # 显示组间均值 fit_means <- data %>% group_by(group) %>% summarize(mean_value = mean(value)) print(fit_means) # 可视化结果 ggplot(data, aes(x = group, y = value)) + geom_boxplot() + geom_point(aes(color = group), position = position_jitter(width = 0.2)) + geom_hline(yintercept = mean(data$value), linetype = "dashed") + geom_text(aes(label = round(mean_value, 2), y = mean_value), data = fit_means, size = 4, vjust = -1) + labs(title = "单因素方差分析", x = "组别", y = "数值") + theme_minimal() ``` 首先,读取Excel数据并存储在`data`数据框中,然后使用`aov()`函数进行单因素方差分析。执行`summary()`函数,可以查看ANOVA分析结果。接着,计算每个组的均值,并将结果存储在`fit_means`数据框中。然后,使用ggplot2绘制箱线图和散点图,箱线图展示每个组的分布情况,散点图展示每个样本的具体数值,并使用颜色区分不同组别。使用`geom_hline()`函数添加总体均值的水平线,使用`geom_text()`函数添加每个组的均值标签。最后,使用`labs()`函数设置标题和坐标轴标签,使用`theme_minimal()`函数设置主题样式。 需要注意的是,箱线图和散点图可以帮助我们理解数据的分布情况,但并不是所有的数据都适合使用这种可视化方法。在实际应用中,需要根据数据的特征和分析目的选择合适的可视化方法。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

统计学小王子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值