r中的堆积条形图与汇总数据 [英] Stacked bar plot in r with summarized data

查看:461
本文介绍了r中的堆积条形图与汇总数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的r。我试图做一个堆积酒吧阴谋。我可以使用barplot函数使其工作。不过,我无法弄清楚如何让图例看起来不错。



这些数据代表了一个模拟比较自举置信区间和基于标准参数置信区间的模拟在t分布上。数据已被汇总。这是我的data.frame的样子:

  test cr type2 
1 Bootstrap 0.406 0.596
2 T检验0.382 0.618

cr =正确拒绝,type2 =类型2错误



我想要做的是制作一个堆叠的条形图,每个测试都有一个条形图。酒吧应该与cr和type2堆积,所以他们的高度应该总结为1。



任何帮助/建议将非常感激!



Espen

解决方案

您可能应该将数据重塑为长格式,

  library(ggplot2)

d < - read.table(textConnection(test cr type2
Bootstrap 0.406 0.596

$ b ggplot(melt(d,id =test),aes(b $ b T-test 0.382 0.618),head = TRUE)

library(reshape2) test,value,fill = variable))+
geom_bar(stat =identity,position =stack)


I'm New to r. Im trying to make a stacked bar plot. I could make it work using the barplot function. However I could not work out how to make the legend look nice. Now i'm trying to use ggplot2 but I really cant make the chart look correctly.

The data represents a simulation comparing a bootstrap confidence interval against a standard parametric confidence interval based on the t-distribution. The data is already summarized. This is how my data.frame look like:

       test    cr type2
1 Bootstrap 0.406 0.596
2    T-test 0.382 0.618

cr = correct rejection, type2 = type 2 errors

What I want to do is to make a stacked bar chart With one bar for each test. The bars should be stacked With cr and type2 so their height should both summarize to 1.

Any help/suggestions would be very appreciated!

Espen

解决方案

You probably should reshape the data to long format,

library(ggplot2)

d <- read.table(textConnection("test    cr type2
Bootstrap 0.406 0.596
T-test 0.382 0.618"),head=TRUE)

library(reshape2)

ggplot(melt(d, id="test"), aes(test, value, fill=variable)) +
  geom_bar(stat="identity", position="stack") 

这篇关于r中的堆积条形图与汇总数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆