前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >双坐标分别绘制条形图和折线图

双坐标分别绘制条形图和折线图

作者头像
生信技能树jimmy
发布2023-09-26 20:31:20
2490
发布2023-09-26 20:31:20
举报
文章被收录于专栏:单细胞天地

加载R包

代码语言:javascript
复制
pkgs = c('tidyverse', 'forcats', 'gtools', 'ggplot2', 'ggpubr', 'cowplot', 
         'scales', 'ggsci', 'viridis', 'hrbrthemes', 'Cairo', 'common')
# install.packages('pkgs')
inst = lapply(pkgs, library, character.only = TRUE)

读取或创建数据

代码语言:javascript
复制
# Build dummy data
data <- data.frame(
  date = as.Date("2019-01-01") + 0:99,
  day = 1:100,
  season = rep(c('spring', 'summer', 'autumn', 'winter'), times = 25),
  temperature = runif(100)/10,
  price = runif(100)*10
)

data %>%
  slice(1:3)
##         date day season temperature    price
## 1 2019-01-01   1 spring 0.006128101 4.149554
## 2 2019-01-02   2 summer 0.089528352 3.729223
## 3 2019-01-03   3 autumn 0.039876862 4.795942

set.seed(111)
df.raw <- data %>%
  slice_sample(n = 20)

绘图

1、绘制条形图

代码语言:javascript
复制
p <- df.raw %>%
  mutate(day = factor(day, levels = unique(mixedsort(day)))) %>%
  mutate(season = factor(season, levels = c('spring', 'summer', 'autumn', 'winter'))) %>%
  ggplot(aes(x = day)) +
  geom_bar(aes(y = temperature, fill = season), stat="identity") 
p

2、条形图分面

代码语言:javascript
复制
# 默认条形图分面
p + facet_grid(.~season)

代码语言:javascript
复制
# 条形图分面宽度相同,只展示使用的因子水平
p + facet_grid(.~season, scale="free") 
代码语言:javascript
复制
# 条形图分面宽度自动调整
p1 <- p + facet_grid(.~season, scale="free", space="free_x") 
p1

3、添加折线图

代码语言:javascript
复制
p2 <- p1 + geom_line(aes(y=price / 100, group = 1), size = 1, linetype = 'dashed') 
p2

4、添加第二坐标轴

代码语言:javascript
复制
p3 <- p2 +  scale_y_continuous(
  name = "First Axis",
  sec.axis = sec_axis(~. * 100, name='Second Axis')) 
p3

5、配色

代码语言:javascript
复制
p4 <- p3 + theme_classic2(base_size = 16) +
  scale_fill_simpsons() + theme(legend.position = 'top')
p4

6、给变量加上标

代码语言:javascript
复制
# 例如给秋天autumn加上标数字1,用函数supsc()
p <- df.raw %>%
  mutate(day = factor(day, levels = unique(mixedsort(day)))) %>%
  mutate(season = factor(season, levels = c('spring', 'summer', 'autumn', 'winter'))) %>%
  mutate(season = recode(season,  "autumn" = paste0("autumn", supsc('1')) ) )  %>%
  ggplot(aes(x = day)) +
  geom_bar(aes(y = temperature, fill = season), stat="identity") 


p + facet_grid(.~season, scale="free", space="free_x") +
  geom_line(aes(y=price / 100, group = 1), size = 1, linetype = 'dashed') +
  scale_y_continuous(name = "First Axis", sec.axis = sec_axis(~. * 100, name='Second Axis')) +
  theme_classic2(base_size = 16) + scale_fill_simpsons() + theme(legend.position = 'top')

参考资料

  • Dual Y axis with R and ggplot2(https://r-graph-gallery.com/line-chart-dual-Y-axis-ggplot2.html)
  • Combine ggplot2 Line & Barchart with Double-Axis in R (2 Examples)(https://statisticsglobe.com/combine-ggplot2-line-barchart-double-axis-r)
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2023-06-30 23:08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 单细胞天地 微信公众号,前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 加载R包
  • 读取或创建数据
  • 绘图
    • 1、绘制条形图
      • 2、条形图分面
        • 3、添加折线图
          • 4、添加第二坐标轴
            • 5、配色
              • 6、给变量加上标
              • 参考资料
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档