具有多个重叠误差条的条形图

发布于 2024-11-02 07:53:24 字数 3221 浏览 2 评论 0原文

我正在尝试创建一个包含多个误差条的条形图。像这样的东西: http://flyordie.sin.khk.be/r /histogram%20error%20bars.PNG 我有以下数据集: http://flyordie.sin.khk.be/r/output.csv

我尝试过使用 ggplot2 和点阵图形,但没有找到适合我需求的东西。

我当前用于显示条形图的代码是这样的:

data <- read.csv("c:/output.csv")
data

par(las=3)
barplot(data$PlateId,
    height=data$HC.Maximum,
    names.arg=data$PlateId,
    col="lightblue")

为了显示最高的错误栏,我使用此代码 库(ggplot2)

limits <- aes(ymax = qc$HC.Maximum, ymin = qc$HC.Minimum)

p <- ggplot(qc, aes(colour=HC.Median,x=PlateId))
p + geom_bar(position="dodge")+ geom_errorbar(limits,position="dodge")

但我不知道如何将它们放在同一个图形上(就像在我的示例中)

数据:

qc <- structure(list(row = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Row", class = "factor"), 
    ID = 1:14, PlateId = c(35276L, 35279L, 35280L, 35281L, 35282L, 
    35290L, 35291L, 35292L, 35293L, 35294L, 35295L, 35296L, 35297L, 
    35298L), LC.Median = c(439688.495, 509376.055, 475218.99, 
    497368.215, 481801.9, 468603.43, 494713.175, 459047.385, 
    482819.47, 495162.31, 449592.51, 460564.95, 478715.915, 452293.465
    ), LC.Stdev = c(52290.12229, 49648.49436, 55743.10306, 62002.53552, 
    46908.66149, 52489.615, 48016.94019, 52082.23899, 47934.37133, 
    58977.84845, 45827.62648, 53514.21095, 49638.98286, 139686.144
    ), LC.Minimum = c(279610.16, 423651.45, 356422.31, 411639.77, 
    397362.84, 345178.07, 406073.72, 352834.86, 339035.77, 369554.11, 
    348688.39, 357341.56, 370463.11, 210367.91), LC.Maximum = c(498195.9, 
    630648.53, 614625.78, 686737.35, 621372.36, 576491.41, 579708.95, 
    580633.28, 580125.9, 622108.73, 530234.87, 563616.65, 614936.33, 
    730272.63), HC.Median = c(507356.465, 553226.525, 447067.77, 
    452223.76, 453439.37, 422491.755, 447438.8, 435034.635, 446148.105, 
    438089.69, 466748.63, 440005.81, 454927.74, 483599.71), HC.Stdev = c(65355.46121, 
    72762.07338, 80118.37641, 43653.99318, 73389.12355, 62590.47601, 
    46421.36678, 62822.88532, 61175.4241, 64418.56174, 63101.2232, 
    68166.51814, 61256.74139, 87354.9441), HC.Minimum = c(381552.05, 
    391124.94, 280614.72, 395454.12, 291433.84, 252579.15, 331661.03, 
    296223.64, 240262.37, 299431.98, 375224.27, 278780.87, 310275.66, 
    213170.04), HC.Maximum = c(626483.6, 635111.41, 555357.3, 
    528822.8, 534172.42, 514927.42, 538385.26, 533024.74, 524973.99, 
    544335.94, 564954.87, 572206.98, 547489.1, 565338.09), zPrime = c(-3.96, 
    -23.73, -7.88, -5.81, -5.32, -5.54, -4.48, -7.98, -6.99, 
    -5.63, -22.54, -33.83, -11.92, -17.44), Sb = c(1.17, 1.03, 
    0.91, 0.91, 0.89, 0.89, 0.9, 0.92, 0.92, 0.89, 1.04, 0.98, 
    0.95, 1.09), Sn = c(1.37, 0.3, -0.83, -0.76, -1.22, -1.01, 
    -1.08, -0.74, -0.86, -0.95, 0.31, -0.2, -0.52, 0.27)), .Names = c("row", 
"ID", "PlateId", "LC.Median", "LC.Stdev", "LC.Minimum", "LC.Maximum", 
"HC.Median", "HC.Stdev", "HC.Minimum", "HC.Maximum", "zPrime", 
"Sb", "Sn"), class = "data.frame", row.names = c(NA, -14L))

I am trying to create a barplot with multiple errorbars. Something like this: http://flyordie.sin.khk.be/r/histogram%20error%20bars.PNG
I have the following dataset:
http://flyordie.sin.khk.be/r/output.csv

I have tried using ggplot2 and lattice graphics, but haven't found anything that suits my needs.

My current code for showing the barchart is this:

data <- read.csv("c:/output.csv")
data

par(las=3)
barplot(data$PlateId,
    height=data$HC.Maximum,
    names.arg=data$PlateId,
    col="lightblue")

And to show the highest errorbars i use this code
library(ggplot2)

limits <- aes(ymax = qc$HC.Maximum, ymin = qc$HC.Minimum)

p <- ggplot(qc, aes(colour=HC.Median,x=PlateId))
p + geom_bar(position="dodge")+ geom_errorbar(limits,position="dodge")

But I have no clue on how to put them on the same graphic (like in my example)

The data:

qc <- structure(list(row = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Row", class = "factor"), 
    ID = 1:14, PlateId = c(35276L, 35279L, 35280L, 35281L, 35282L, 
    35290L, 35291L, 35292L, 35293L, 35294L, 35295L, 35296L, 35297L, 
    35298L), LC.Median = c(439688.495, 509376.055, 475218.99, 
    497368.215, 481801.9, 468603.43, 494713.175, 459047.385, 
    482819.47, 495162.31, 449592.51, 460564.95, 478715.915, 452293.465
    ), LC.Stdev = c(52290.12229, 49648.49436, 55743.10306, 62002.53552, 
    46908.66149, 52489.615, 48016.94019, 52082.23899, 47934.37133, 
    58977.84845, 45827.62648, 53514.21095, 49638.98286, 139686.144
    ), LC.Minimum = c(279610.16, 423651.45, 356422.31, 411639.77, 
    397362.84, 345178.07, 406073.72, 352834.86, 339035.77, 369554.11, 
    348688.39, 357341.56, 370463.11, 210367.91), LC.Maximum = c(498195.9, 
    630648.53, 614625.78, 686737.35, 621372.36, 576491.41, 579708.95, 
    580633.28, 580125.9, 622108.73, 530234.87, 563616.65, 614936.33, 
    730272.63), HC.Median = c(507356.465, 553226.525, 447067.77, 
    452223.76, 453439.37, 422491.755, 447438.8, 435034.635, 446148.105, 
    438089.69, 466748.63, 440005.81, 454927.74, 483599.71), HC.Stdev = c(65355.46121, 
    72762.07338, 80118.37641, 43653.99318, 73389.12355, 62590.47601, 
    46421.36678, 62822.88532, 61175.4241, 64418.56174, 63101.2232, 
    68166.51814, 61256.74139, 87354.9441), HC.Minimum = c(381552.05, 
    391124.94, 280614.72, 395454.12, 291433.84, 252579.15, 331661.03, 
    296223.64, 240262.37, 299431.98, 375224.27, 278780.87, 310275.66, 
    213170.04), HC.Maximum = c(626483.6, 635111.41, 555357.3, 
    528822.8, 534172.42, 514927.42, 538385.26, 533024.74, 524973.99, 
    544335.94, 564954.87, 572206.98, 547489.1, 565338.09), zPrime = c(-3.96, 
    -23.73, -7.88, -5.81, -5.32, -5.54, -4.48, -7.98, -6.99, 
    -5.63, -22.54, -33.83, -11.92, -17.44), Sb = c(1.17, 1.03, 
    0.91, 0.91, 0.89, 0.89, 0.9, 0.92, 0.92, 0.89, 1.04, 0.98, 
    0.95, 1.09), Sn = c(1.37, 0.3, -0.83, -0.76, -1.22, -1.01, 
    -1.08, -0.74, -0.86, -0.95, 0.31, -0.2, -0.52, 0.27)), .Names = c("row", 
"ID", "PlateId", "LC.Median", "LC.Stdev", "LC.Minimum", "LC.Maximum", 
"HC.Median", "HC.Stdev", "HC.Minimum", "HC.Maximum", "zPrime", 
"Sb", "Sn"), class = "data.frame", row.names = c(NA, -14L))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与往事干杯 2024-11-09 07:53:24

创建具有多个图层的绘图时,我的方法如下:

  1. 在对 ggplot 的初始调用中定义常见的美感
  2. 在每个附加层中定义附加的美感

请注意,我已经修改了您的代码,因为我无法获取您的示例工作:

  1. geom_bar 提供显式 binwidth=1 以删除警告
  2. 删除 position=dodge 因为这是默认且多余的
  3. 提供明确的stat=identitygeom_bar

代码:

ggplot(qc, aes(x=PlateId)) + 
  geom_bar(aes(y=HC.Median), binwidth=1, stat="identity", fill="cyan") + 
  geom_errorbar(aes(ymin=HC.Minimum, ymax=LC.Minimum), colour="red") + 
  geom_errorbar(aes(ymin=LC.Maximum, ymax=HC.Maximum), colour="purple") 

在此处输入图像描述

When creating plots with multiple layers, I approach it as follows:

  1. Define common aesthetics in the initial call to ggplot
  2. Define additional aesthetics in each additional layer

Note that I have modified your code, since I couldn't get your example to work:

  1. Provide an explicit binwidth=1 to geom_bar to remove the warnings
  2. Remove the position=dodge since this is the default and redundant
  3. Supply an explicit stat=identity to geom_bar

The code:

ggplot(qc, aes(x=PlateId)) + 
  geom_bar(aes(y=HC.Median), binwidth=1, stat="identity", fill="cyan") + 
  geom_errorbar(aes(ymin=HC.Minimum, ymax=LC.Minimum), colour="red") + 
  geom_errorbar(aes(ymin=LC.Maximum, ymax=HC.Maximum), colour="purple") 

enter image description here

以往的大感动 2024-11-09 07:53:24

只需添加另一个geom_errorbar

#Rename limits to limits_hi
limits_hi <- aes(ymax = qc$HC.Maximum, ymin = qc$HC.Minimum)

#Define the other error bar
limits_lo <- aes(ymax = qc$LC.Maximum, ymin = LC.Minimum)

#I'm not quite sure what you want in the bars; see if this looks right
p <- ggplot(qc, aes(factor(PlateId), HC.Median)) 

p +
  geom_bar(position="dodge") + 
  geom_errorbar(limits_hi, position="dodge", colour = "red") + 
  geom_errorbar(limits_lo, position="dodge", colour = "blue") +
  opts(axis.text.x = theme_text(angle = 30))

Just add another geom_errorbar.

#Rename limits to limits_hi
limits_hi <- aes(ymax = qc$HC.Maximum, ymin = qc$HC.Minimum)

#Define the other error bar
limits_lo <- aes(ymax = qc$LC.Maximum, ymin = LC.Minimum)

#I'm not quite sure what you want in the bars; see if this looks right
p <- ggplot(qc, aes(factor(PlateId), HC.Median)) 

p +
  geom_bar(position="dodge") + 
  geom_errorbar(limits_hi, position="dodge", colour = "red") + 
  geom_errorbar(limits_lo, position="dodge", colour = "blue") +
  opts(axis.text.x = theme_text(angle = 30))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文