Extending error bars in both directions (non-symmetrical), base R, Dynamite Plot

huangapple go评论73阅读模式
英文:

Extending error bars in both directions (non-symmetrical), base R, Dynamite Plot

问题

我已经编写了一段代码来显示我喜欢的炸药图(我修改了在GitHub上找到的一个函数)。

我已经为示例创建了一个模拟数据集,并且一直在尝试调整代码,以使误差条不仅在每个条形图上方,还在每个条形图下方延伸。这已经实现了。然而,二项模型的误差不总是对称的,我想调整这个

假设,对于这个示例,我希望下面的图中处理T2的误差为+8和-13。这该如何实现?

以下是我一直在使用的这个特定示例的代码:

#这是炸药图函数
dynamitePlot <- function(height, error, names = NA, significance = NA, ylim = c(0,maxLim), ...){
  maxLim <- 1.1* max(mapply(sum, height, error))
  bp <- barplot(height, names.arg = names, ylim = ylim, ...)
  arrows(x0 = bp, y0 = height, y1 = height + error, angle = 90)
  arrows(x0 = bp, y0 = height, y1 = height - error, angle = 90)
  text(x = bp, y = 2 + height + error, labels = significance, cex=1.5)
}


#试验数据
trial <- data.frame( "cats" = character(0))
x <- c("T2", "T1", "C")
df <- data.frame("cats" = x)

#炸药图的值
Values <- c(72,60,20) # y轴上的百分比
Errors <- c(8,13,12) # 标准误差(**这里可能有问题**(我认为),假设我想要误差条分别从(4:8),(3:13),(6:12)延伸到每个“treatment”(T2,T1和C))
Names <- df$cats
Sig <- c("A","A","B") # 表示统计显著性的字母(这里没有显示测试)

par(mar=c(5,5,2,1))
pallet <- c('111','222','333') 

#创建图
dynamitePlot(Values, Errors, significance = Sig, names = df$cats,col=pallet, width=c(0.03,0.03,0.03,0.03,0.03), ylim = c(0,100), cex.axis=1.5, cex.lab=1.5,ylab="Percent [%]", xlab="Treatment")

这是我当前得到的结果:

Extending error bars in both directions (non-symmetrical), base R, Dynamite Plot

英文:

I have written a code to display a dynamite plot the way I like (I modified a function I found on github)

I have made a mock data set for the example, and have been playing around with the code to try and get the error bars to not only go above each bar plot, but to extend below each bar plot as well. This has been achieved. However, the error from binomial models is not always symmetrical, and I would like to adjust for this.

Lets say, for this example, I would like the error around treatment T2 in the graph below to be +8 and -13. How could this be achieved?

Here is the code for this particular example that I have been working with:

#here is the dynamite plot function
dynamitePlot &lt;- function(height, error, names = NA, significance = NA, ylim = c(0,maxLim), ...){
  maxLim &lt;- 1.1* max(mapply(sum, height, error))
  bp &lt;- barplot(height, names.arg = names, ylim = ylim, ...)
  arrows(x0 = bp, y0 = height, y1 = height + error, angle = 90)
  arrows(x0 = bp, y0 = height, y1 = height - error, angle = 90)
  text(x = bp, y = 2 + height + error, labels = significance, cex=1.5)
}


#Trial data
trial &lt;- data.frame( &quot;cats&quot; = character(0))
x &lt;- c(&quot;T2&quot;, &quot;T1&quot;, &quot;C&quot;)
df &lt;- data.frame(&quot;cats&quot; = x)

#Values for dynamite plot
Values &lt;- c(72,60,20) # % on y Axis
Errors &lt;- c(8,13,12) # the standard errors (**this is where the problem is** (I think), lets pretend I want the error bars to extend from (4:8), (3:13), and (6:12) for each &quot;treatment&quot; respectively (T2, T1, and C)
Names &lt;- df$cats
Sig &lt;- c(&quot;A&quot;,&quot;A&quot;,&quot;B&quot;) # letters denoting statistical significance (test not shown here)



par(mar=c(5,5,2,1))
pallet &lt;- c(&#39;111&#39;,&#39;222&#39;,&#39;333&#39;) 

#make plot
dynamitePlot(Values, Errors, significance = Sig, names = df$cats,col=pallet, width=c(0.03,0.03,0.03,0.03,0.03), ylim = c(0,100), cex.axis=1.5, cex.lab=1.5,ylab=&quot;Percent [%]&quot;, xlab=&quot;Treatment&quot;)

Here is what I currently get
Extending error bars in both directions (non-symmetrical), base R, Dynamite Plot

答案1

得分: 1

另一个在dynamitePlot()中的arrow()行可以显示下半部分:

arrows(x0 = bp, y0 = height, y1 = height - error, angle = 90)
英文:

Another arrow() line in dynamitePlot() can display the lower half:

arrows(x0 = bp, y0 = height, y1 = height - error, angle = 90)

huangapple
  • 本文由 发表于 2023年5月22日 20:13:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76306082.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定