好奇心Log 发表于 2024-3-6 15:00:49

你的柱状图out了!看看Nature是怎么画的——柱状图+散点图+误差线!

Part1你的柱状图out了!看看Nature是怎么画的——柱状图+散点图+误差线!


在日常的研究中,可能仅仅绘制柱状图就完事了,让我们看看Nature子刊是如何呈现的吧。柱状图+散点图+误差线!


                                                            Nature子刊


主要复现这篇文章中的Fig 7中的柱状图子图:


                                                                   Nature子刊的柱状图


本文介绍如何使用R语言和ggplot2包将简单的柱状图转化为更为高级和信息丰富的视觉表示形式。通过结合柱状图、散点图和误差线,结果展示出一个全面和精确的数据视图,这种方法在Nature等顶级科学期刊中经常见到。

1设置环境
首先,我们需要加载必要的R包来处理数据和生成图形:

这些包提供了数据处理和图形创建的强大功能,是进行高级数据可视化的基础。

library(ggplot2)
library(dplyr)
2读取绘图数据
接下来,我们从指定的CSV文件中读取数据:

df1包含了柱状图所需的平均值数据,而df2则包含了散点图和误差线所需的原始数据和误差信息。

如果有同学想尝试代码,可以文末获取数据+代码

df1 <- read.csv('data/df1.csv')
df2 <- read.csv('data/test_err.csv')
3绘图
使用ggplot2的绘图代码,我们可以创建一个结合了柱状图、散点图和误差线的复合图形:

ggplot() +
geom_bar(data = df1, aes(x = type, y = mean), fill = "white", size = 2,
         color = c("#4169B2", "#B1A4C0", "#479E9B", "#BB2BA0"),
         position = position_dodge(width = 0.65), stat = "identity", width = 0.65) +
geom_jitter(data = df2, aes(x = type, y = value, fill = type, colour = type), size = 2,height = 0.02,width = 0.1) +
scale_color_manual(values = c("#4169B2", "#B1A4C0", "#479E9B", "#BB2BA0")) +
geom_errorbar(data = df1, aes(x = type, ymin = mean-se, ymax = mean+se), width = 0.3,
                color = c("#4169B2", "#B1A4C0", "#479E9B", "#BB2BA0"), size = 1.5,
                position = position_dodge(width = 0.65)) +
labs(y = "Mobile Phone sales", x = "") +
geom_hline(aes(yintercept = 5), linetype = "dashed", linewidth = 1.2, colour = "gray56") +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
      text = element_text(family = "serif", size = 15), # This sets the font family and size globally
      axis.text.x = element_text(size = 18), # Larger font size for X axis text
      axis.text.y = element_text(size = 18), # Larger font size for Y axis text
      axis.title.x = element_text(size = 20), # Larger font size for X axis title
      axis.title.y = element_text(size = 20)) # Larger font size for Y axis title

                                                代码绘制结果
此代码段创建了一个复杂的图形,不仅展示了每种类型的平均值数据,而且通过散点图展示了个体数据点,同时误差线提供了数据的变异性信息。使用不同颜色来区分不同的类型,增加了图形的清晰度和吸引力。

4结语
通过这种方法,我们不仅能够展示数据的中心趋势,如平均值,还能展示数据的分布和可靠性。这种图形表达方式在科学研究和报告中极为重要,因为它提供了一个全面且准确的数据视图,有助于读者更好地理解和评估研究结果。

学习了本篇后,再也不用烦恼只会绘制简单的柱状图了!


本文转自微信公众号:好奇心Log

FrankJScott 发表于 2024-9-6 00:28:25

Awesome Daily Sport Predictions Info

For the lady asking about mls predictions this weekend, table tennis computer predictions, best nfl betting promo codes, tennis predictions today straight up, tennis h2h news & results today, best american football nfl picks & parlays, winning cricket picks strategy, estrategia tenis pronosticos hoy, camel racing picks against the spread, expert baseball mlb predictions,I highly suggest this new useful sport prediction tips or computadora tenis pronostico estrategia, table tennis predictions, darts predictions this weekend, american football nfl picks against the spread, nfl tips picks for today, winning h2h stats picks today, ice hockey nhl news today, winning table tennis picks strategy, nfl predictions, mejor tenis pronostico systema, on top of this a replacement about updated sport prediction tips as well as winning baseball mlb picks strategy, nfl predictions, american football nfl prediction news today, nfl & nba predictions tomorrow, nfl tips picks straight up, camel racing picks today, cricket predictions this weekend, free h2h stats picks today & tomorrow, tennis prediction news today, best american football nfl predictions platform, as well as this helpful resources for updated sport prediction forum which is also great. Also, have a look at this learn more on awesome sport prediction advice not to mention basketball nba picks for today, nfl betting promo codes, crypto casino bonus codes usa, mls computer predictions, ice hockey nhl prediction news of the day, tennis picks straight up, baseball mlb predictions for this week, daily picks for soccer mls, daily picks for h2h stats, tennis picks tomorrow, on top of this cool top sport prediction info with latest nfl betting promo codes, nfl & nba picks, tennis picks of the day, nfl predictions maker, best darts picks & parlays, tenis ai pronosticos, best ice hockey nhl betting picks site, espn / draftking nfl promo codes, baseball mlb picks, darts picks each week,for good measure. Check more @ Top UG ZEUS Blog 69c2cde
页: [1]
查看完整版本: 你的柱状图out了!看看Nature是怎么画的——柱状图+散点图+误差线!