跟着Nature正刊学作图 | 双轴柱状+折线散点图!

2023-11-01 02:20

本文主要是介绍跟着Nature正刊学作图 | 双轴柱状+折线散点图!,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

📋文章目录

  • 复现图片
  • 设置工作路径和加载相关R包
  • 读取数据集
  • 数据可视化
  • 计算均值和标准差
  • 可视化过程

   跟着「Nature」正刊学作图,今天复现Nature文章中的一张双轴图–左边为分组柱状图、右边为折线散点图。

复现图片

在这里插入图片描述
图中的a是我们今天准备复刻的,该图由柱状图和散点图组合的双轴图。

设置工作路径和加载相关R包

rm(list = ls()) # 清空当前环境变量
setwd("C:/Users/Zz/Desktop/公众号 SES") # 设置工作路径
# 加载R包
library(ggplot2)
library(tidyverse)

读取数据集

cData <- read_csv("cData.csv")
head(cData)
# Weeks Type               lfValue rgValue
# <dbl> <chr>                <dbl>   <dbl>
# 1    20 By week of testing    2500    1.3 
# 2    20 By week of testing    2550    1.5 
# 3    20 By week of testing    2450    1.45
# 4    21 By week of testing    2750    1.2 
# 5    21 By week of testing    2780    1.25
# 6    21 By week of testing    2680    1.18

数据可视化

# 物种组成堆叠面积图
library(ggplot2)
library(ggalluvial)
ggplot(data = top10,aes(x = Depth, y = Abundance, fill = reorder(Phylum, -Abundance),colour = reorder(Phylum, -Abundance),stratum = reorder(Phylum, -Abundance) ,alluvium = reorder(Phylum, -Abundance))) +geom_alluvium(aes(fill = reorder(Phylum, -Abundance)), alpha = 0.7, decreasing = FALSE) +geom_stratum(aes(fill = reorder(Phylum, Abundance)), width = 0.3, size = 0.1, color = "black") +scale_y_continuous(expand = c(0, 0)) +theme_bw() +facet_grid(. ~ Treat, scales = "fixed") +scale_fill_manual(values = c("#EB7369", "#CF8B0B", "#9D9F20", "#2BB077", "#2BB077","#1BB3B7", "#29A4DE", "#8989C1", "#B174AD","#DE66A1"), name =  "Phylum") +scale_color_manual(values = c("#EB7369", "#CF8B0B", "#9D9F20", "#2BB077", "#2BB077","#1BB3B7", "#29A4DE", "#8989C1", "#B174AD","#DE66A1")) +guides(color = "none")+theme(panel.grid=element_blank(),panel.spacing.x = unit(0, units = "cm"),strip.background = element_rect(color = "white", fill = "white", linetype = "solid", size = 1),strip.placement = "outside",axis.line.y.left = element_line(color = "black", size = 0.7),axis.line.x.bottom = element_line(color = "black", size = 0.7),strip.text.x = element_text(size = 14, face = "bold"),axis.text = element_text(face = "bold", size = 12, color = "black"),axis.title = element_text(face = "bold", size = 14, colour = "black"),legend.title = element_text(face = "bold", size = 12, color = "black"),legend.text = element_text(face = "bold", size = 12, color = "black"),axis.ticks.x = element_line(size = 1),axis.ticks.y = element_line(size = 1),)+labs(x = "Depth",y= "Relative Abundance of Phylum (%)")

数据包括以下指标:2个(左边和右边)数值变量、2个分类变量。

在可视化前,我们需要先思考图中构成的元素,由哪些组成。

  • 计算每个分组或处理下的均值和标准差;

计算均值和标准差

cData_summary <- cData %>%group_by(Weeks, Type) %>%summarise(avg_lfValue = mean(lfValue),sd_lfValue = sd(lfValue),avg_rgValue = mean(rgValue),sd_rgValue = sd(rgValue),)
cData_summary
# Weeks Type               avg_lfValue sd_lfValue avg_rgValue sd_rgValue
# <dbl> <chr>                    <dbl>      <dbl>       <dbl>      <dbl>
# 1    20 By week of onset         2623.       25.2        1.98     0.0764
# 2    20 By week of testing       2500        50          1.42     0.104 
# 3    21 By week of onset         3543.       40.4        1.74     0.0361
# 4    21 By week of testing       2737.       51.3        1.21     0.0361
# 5    22 By week of onset         2770        26.5        1.28     0.0300
# 6    22 By week of testing       2160        60          1.10     0.0839
# 7    23 By week of onset         2143.       40.4        1.31     0.0208
# 8    23 By week of testing       1777.       75.1        1.02     0.0153
# 9    24 By week of onset         1823.       25.2        1.15     0.0300
# 10    24 By week of testing       1667.       61.1        1.07     0.0265
# 11    25 By week of onset         1690        36.1        1.23     0.0208
# 12    25 By week of testing       1610        36.1        1.2      0.0300
# 13    26 By week of onset         1607.       30.6        1.18     0.0252
# 14    26 By week of testing       1673.       30.6        1.16     0.0361

可视化过程

ggplot()+geom_bar(data = cData_summary %>% mutate(Type = factor(Type, levels = c("By week of testing","By week of onset"))),aes(x = Weeks, y = avg_lfValue, fill = Type), alpha = 0.5, stat = "identity", position = position_dodge(0.75), width = 0.75) +geom_errorbar(data = cData_summary %>% mutate(Type = factor(Type, levels = c("By week of testing","By week of onset"))),aes(x = Weeks, y = avg_lfValue, ymin = avg_lfValue - sd_lfValue, ymax = avg_lfValue + sd_lfValue,group = Type), color = "black",position = position_dodge(0.75), width = 0.2) +geom_line(data = cData_summary %>% mutate(Type = factor(Type, levels = c("By week of testing","By week of onset"))),aes(x = Weeks, avg_rgValue*1950, group = Type, color = Type),position = position_dodge(0.75), linewidth = 0.8) +geom_point(data = cData_summary %>% mutate(Type = factor(Type, levels = c("By week of testing","By week of onset"))),aes(x = Weeks, y = avg_rgValue*1950, color = Type), position = position_dodge(0.75), size = 2.5) + scale_x_continuous(breaks = seq(20, 26, 1)) +scale_y_continuous(name = c("Number of laboratory-confirmed\n sympotomatic cases"),sec.axis = sec_axis(~ ./1950, name = c("Test positivity rate (%)"),breaks = seq(0, 2, 1)),limits = c(0, 4000),breaks = seq(0, 4000, 500),expand = c(0, 0)) +scale_color_manual(values = c("#FE8F3C", "#1E899A")) +scale_fill_manual(values = c("#FE8F3C", "#1E899A")) +theme_bw() +theme(legend.position = c(0.9, 0.9),legend.background = element_blank(),panel.grid.major = element_blank(),panel.grid.minor = element_blank(),axis.text.x.bottom = element_text(color = "black", size = 12),axis.text.y.left = element_text(color = "black", size = 12),axis.text.y.right = element_text(color = "#44909A", size = 12),axis.title.y.right = element_text(color = "#44909A", size = 12, angle = 90),axis.line.y.right = element_line(color = "#44909A"),axis.ticks.y.right = element_line(color = "#44909A"),axis.title = element_text(color = "black", size = 12)) +labs(x = "Week",color = "",fill = "")

在这里插入图片描述

复现效果比较完美,细节可以参考文中代码,有疑惑可以留言讨论~

这篇关于跟着Nature正刊学作图 | 双轴柱状+折线散点图!的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/319782

相关文章

使用matplotlib绘制散点图、柱状图和饼状图-学习篇

一、散点图 Python代码如下: num_points = 100x = np.random.rand(num_points) #x点位随机y = np.random.rand(num_points) #y点位随机colors = np.random.rand(num_points) #颜色随机sizes = 1000 * np.random.rand(num_points) # 大

【python 散点图】美观画时间序列散点图

经常遇到时间序列的数据,用散点图可以直观的查看数据的分布情况。matplotlib模块的pyplot有画散点图的函数,但是该函数要求x轴是数字类型。pandas的plot函数里,散点图类型’scatter’也要求数字型的,用时间类型的会报错。 最终摸索出画散点图的简单办法。可以使用pyplot的plot_date()画散点图。 # -*- coding: utf-8 -*-"""spee

Nature子刊:教你零基础开展微生物组数据分析和可视化

使用MicrobiomeAnalyst进行微生物组数据的全面统计、功能和元分析 Using MicrobiomeAnalyst for comprehensive statistical, functional, and meta-analysis of microbiome data Nature Protocols Impact Factor 11.334 https://do

跟着李沐学ai

01 课程安排【动手学深度学习v2】-跟李沐学AI-【完结】动手学深度学习 PyTorch版-哔哩哔哩视频 (bilibili.com)https://www.bilibili.com/list/1567748478?sid=358497&spm_id_from=333.999.0.0&desc=1&oid=714717789&bvid=BV1oX4y137bC 目标 介绍深度学习经典和最新模

【科普】双轴测径仪是根据哪个测量值控制外径尺寸?

单轴测径仪与双轴测径仪都是自带闭环控制功能的在线外径测量设备,单轴测径仪只有一个测头,是根据该测头的检测数据进行控制,这点毋庸置疑,那双轴测径仪这种具备两组测头的设备又是如何控制的,本文就来简单的介绍一下。 JG02-DG系列双通道测径仪内置2组固定式光电测头,可对被测物两个方向的外径尺寸进行实时测量。主要应用于BV线、通讯电缆、塑胶线、电力电缆、光纤、漆包线、铝塑管、钢材、纤维等各类管材、棒材、

Nature Communications:解码人类触觉感知与运动神经控制机理,用仿生手重现类人触觉感知与抓握

近日,由曼彻斯特大学、牛津大学、吉林大学、索尔福德大学等多所机构组成的国际研究团队,在Nature Communications期刊上发表了一篇重要研究成果,题为Human tactile sensing and sensorimotor mechanism: from afferent tactile signals to efferent motor control。该研究首次结合人体神经传导

HDU2050.折线分割平面

【题意】 平面上有n条折线,问这些折线最多能将平面分割成多少块? 【思路】 递推公式:F(n)=F(n-1)+4(n-1)+1;通项公式:Zn = 2n(2n+1)/2+1-2n=2n^2–n+1 #include<stdio.h>int main(){int m,n;__int64 c;scanf("%d",&n);while(n--){scanf("%d",&m);c=2*(m*m

Nature:我叫“P值” 这是我的故事

Nature:我叫“P值” 这是我的故事 2014-2-28 09:48| 发布者: slytjiaofei| 查看: 140| 评论: 2|来自: 果壳 摘要: 衡量统计真实性的“黄金标准”——P值,并非众多科学家想象的那样可靠。 2010年某个瞬间,马特·莫德尔(Matt Motyl)离享受科学荣誉仅有一步之遥。那时,他发现政治极端主义者看到的世界是确实是非黑即白的。

【高校科研前沿】加州理工学院Brendan Byrne等人在Nature 正刊发文:2023年加拿大野火的碳排放

论文名称:Carbon emissions from the 2023 Canadian wildfires(2023年加拿大野火的碳排放) 第一作者及单位:Brendan Byrne(碳循环科学家|加州理工学院) 通讯作者及单位:Brendan Byrne(碳循环科学家|加州理工学院) 文章发表期刊:《Nature》(中科院1区Top期刊|最新影响因子:50.5) 期刊

两款在线作图软件

1.processOn https://www.processon.com/diagrams 这个不仅可以可以画流程图,还可以花思维导图。 2.diagrams https://app.diagrams.net/