前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R绘图|ggplot自定义主题一包搞定

R绘图|ggplot自定义主题一包搞定

作者头像
小汪Waud
发布2023-02-16 16:42:35
7390
发布2023-02-16 16:42:35
举报
文章被收录于专栏:小汪Waud小汪Waud

在用RStudio的ggplot2包进行画图时常常记不住对应的美化代码,检索又要花费大量的时间。今天给大家推荐一款非常容易上手,且能够解决大家记不住代码痛点的RStudio扩展R包ggThemeAssist。

ggThemeAssist[1]是一个RStudio的扩展R包,提供了一种用于修改ggplot2主题的图形交互式界面。

安装

在安装ggThemeAssist之前,需要安装稳定或者最新的RStudo(v0.99.878 or later)。此外,ggThemeAssist依赖于shiny和miniUI,如果本地尚未安装,通过BiocManager::install()安装即可。

Install from Github

代码语言:javascript
复制
if (!requireNamespace("devtools", quietly = TRUE))   
install.packages("devtools") 
devtools::install_github("calligross/ggthemeassist")

Install from CRAN

代码语言:javascript
复制
install.packages("ggThemeAssist") # 或BiocManager::install("ggThemeAssist")

使用

用法非常的简单,下载安装后先重启RStudio,选中一段ggplot的作图代码,在RStuido的Addins按钮处即可打开该插件。

可以调节的内容有图形的比例(Settings)、调色板和背景(Panel&Background)、坐标轴(Axis)、标题和标签(Title and label)、图例(Legend)、副标题和说明文字(Subtitle and Caption)

设置好以后,点击右上角的“Done”即可在原作图代码的基础上添加修改代码,重新运行作图代码即可完成修改。

演示

示例数据

本次我们采用ggplot2的内置数据diamonds,考虑到diamonds的数据太多,我们随机选出部分做演示。

代码语言:javascript
复制
library(ggplot2)
library(dplyr) # 需要使用dplyr提取数据
data("diamonds")
small_dia = sample_n(diamonds,size = 1000) # 随机取出1000个数据

作图

我们以carat为x,price为y,cut映射给color作一个点图。

代码语言:javascript
复制
ggplot(data = small_dia,
       aes(x= carat,y=price,color = cut))+
  geom_point()

主题修改

选中作图代码并打开ggThemeAssist,在里面进行修改,为了有较大区分,我特意选择了差异很大的颜色。

调色板和背景(Panel&Background)
代码语言:javascript
复制
ggplot(data = small_dia,
       aes(x= carat,y=price,color = cut))+
  geom_point() + theme(panel.grid.major = element_line(linetype = "dashed"),
    panel.background = element_rect(fill = "cornflowerblue"),
    plot.background = element_rect(fill = "antiquewhite"))

调色板和背景(Panel&Background)

坐标轴(Axis)
代码语言:javascript
复制
ggplot(data = small_dia,
       aes(x= carat,y=price,color = cut))+
  geom_point() + theme(axis.line = element_line(colour = "bisque1",
    size = 3, linetype = "dashed"), axis.ticks = element_line(size = 2),
    axis.text = element_text(size = 14, colour = "cornflowerblue"),
    axis.text.x = element_text(family = "mono",
        size = 14), axis.text.y = element_text(size = 14))

坐标轴(Axis)

标题和标签(Title and label)
代码语言:javascript
复制
ggplot(data = small_dia,
       aes(x= carat,y=price,color = cut))+
  geom_point() + theme(axis.title = element_text(face = "italic",
    colour = "brown3", angle = 45), plot.title = element_text(family = "serif",
    size = 20, face = "bold", hjust = 0.5)) +labs(title = "this is the main title", x = "caratttttttt",
    y = "priceeeeeeeee")

标题和标签(Title and label)

图例(Legend)
代码语言:javascript
复制
ggplot(data = small_dia,
       aes(x= carat,y=price,color = cut))+
  geom_point() + theme(legend.text = element_text(family = "mono"),
    legend.title = element_text(size = 20,
        face = "bold", family = "mono", colour = "brown4"),
    legend.key = element_rect(fill = "hotpink",
        linetype = "solid"), legend.background = element_rect(fill = "gray90"),
    legend.position = "bottom", legend.direction = "horizontal")

图例(Legend)

副标题和说明文字(Subtitle and Caption)
代码语言:javascript
复制
ggplot(data = small_dia,
       aes(x= carat,y=price,color = cut))+
  geom_point() + theme(plot.subtitle = element_text(size = 20,
    colour = "gray26"), plot.caption = element_text(family = "serif",
    size = 20, face = "italic")) +labs(subtitle = "the subtitle", caption = "This is a very beautiful pic")

副标题和说明文字(Subtitle and Caption)

还不抓紧去试试! 参考资料

[1] ggThemeAssist: https://github.com/calligross/ggthemeassist

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-12-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 小汪Waud 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装
    • Install from Github
      • Install from CRAN
      • 使用
      • 演示
        • 示例数据
          • 作图
            • 主题修改
              • 调色板和背景(Panel&Background)
              • 坐标轴(Axis)
              • 标题和标签(Title and label)
              • 图例(Legend)
              • 副标题和说明文字(Subtitle and Caption)
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档