Stacked bar plot in r with summarized data

南笙酒味 提交于 2019-12-11 18:18:53

问题


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


回答1:


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") 


来源:https://stackoverflow.com/questions/16630069/stacked-bar-plot-in-r-with-summarized-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!