2016-09-27 86 views
0

我试图在堆叠的栏上绘制错误栏。堆栈栏上的错误栏ggplot2 R

我有两个数据矩阵,一个是平均值,另一个是标准dev。

> m_mean 
        TP' TP* FP 
high/60   5.445 6.162 1.673 
high/120  5.436 5.098 0.942 
high/180  5.060 3.576 0.242 
high/210  4.705 2.709 0.105 
medium high/60 4.915 6.462 2.086 
medium high/120 4.605 5.652 1.178 
medium high/180 3.940 4.219 0.479 
medium high/210 3.440 3.412 0.208 
medium/60  3.690 7.330 2.290 
medium/120  3.021 6.375 1.611 
medium/180  2.634 4.712 0.659 
medium/210  2.134 3.800 0.482 
low/60   1.626 7.060 3.652 
low/120   1.268 5.626 2.464 
low/180   0.960 3.946 1.379 
low/210   0.703 2.942 1.084 
    > m_devs 
         TP'  TP*  FP 
high/60   1.7291515 1.738614 1.5577219 
high/120  1.6925628 1.718531 1.0429399 
high/180  1.6664865 1.610844 0.4976783 
high/210  1.5704908 1.434705 0.3347941 
medium high/60 1.7125150 1.785986 1.7854388 
medium high/120 1.6774352 1.762953 1.1303069 
medium high/180 1.5921479 1.666078 0.7484111 
medium high/210 1.5845854 1.490798 0.4847380 
medium/60  1.6830724 1.751619 1.7569824 
medium/120  1.4651641 1.806000 1.2865980 
medium/180  1.4373970 1.672679 0.8375085 
medium/210  1.2765867 1.527198 0.7498255 
low/60   1.2568626 1.793772 2.0845242 
low/120   1.0475081 1.878204 1.5630570 
low/180   0.9794689 1.666692 1.1818442 
low/210   0.7882972 1.518862 1.0059602 

我改变两个矩阵从WIDE格式,以LONG

library(reshape2) 
    table2 <- melt(m_mean) 
    table3 <- melt(m_devs) 
    colnames(table2) <- c("Combination", "Legend", "Mean") 
    colnames(table3) <- c("Combination", "Legend", "SD") 
    attach(table2) 
    attach(table3) 

我得出使用ggplot2

library(ggplot2) 
    gg<-ggplot(table2, aes(x = Combination, y = Mean, fill = Legend)) + 
    geom_bar(stat = "identity")+ scale_fill_grey()+ 
    theme_light()+ 

    labs(title = "MAIN TITLE")+ 
    theme(axis.text.y = element_text(face="bold", vjust=0.5, size=10), 
      axis.text.x = element_text(face="bold", vjust=1, size=10,angle = 30, hjust = 1))+ 
    scale_y_continuous(limits=c(0, 14),breaks = seq(0,14,2)) 

我试图绘制误差棒以下一些有用的曲线图回答例如: https://stackoverflow.com/search?q=stacked+bars+errors+ggplot 但我不知道第二就是我不正确的值和正确的位置绘制误差条

library(ggplot2) 
    gg<-ggplot(table2, aes(x = Combination, y = Mean, fill = Legend)) + 
    geom_bar(stat = "identity")+ scale_fill_grey()+ 
    theme_light()+ 

    labs(title = paste("STARS outputs for each combination magnitude/cut-off","\n", 
         "N(8,1) time series, regime length = ", toString(lunghezza),sep=""))+ 
    theme(axis.text.y = element_text(face="bold", vjust=0.5, size=10), 
      axis.text.x = element_text(face="bold", vjust=1, size=10,angle = 30, hjust = 1))+ 
    scale_y_continuous(limits=c(0, 14),breaks = seq(0,14,2)) 
    # Define the top and bottom of the errorbars 
    gg <- gg + geom_errorbar(aes(ymax = Mean + SD, ymin=Mean - SD), 
          position = "identity", width = 1) 
    gg 

这是阴谋

enter image description here

+0

Errorbars看起来不错(好y位置),但你的酒吧堆叠。你真的需要堆垛杆吗?如果你需要它们,你可以为每个误差条计算新的y值(平均值),例如TP'y应该是(平均TP')+(平均TP *)而不是(平均TP *)。 – bVa

+0

我需要将三个参数(TP',TP *和FP)显示为堆积条。 我想绘制每个栏的误差栏,所以每栏有3个误差栏。 –

回答

0

正如我评论,你可以直接计算每个errorbar新y_position :

library(plyr) 
set.seed(1) 
df = data.frame(group = sample(LETTERS[1:3], 100,replace = T), 
      var1 = rnorm(100,10,3), 
      var2 = rnorm(100,20,5), 
      var3 = rnorm(100,50,10)) 

m_mean = ddply(df, "group", summarize, var1 = mean(var1), var2 = mean(var2), var3 = mean(var3)) 
m_devs = ddply(df, "group", summarize, var1 = sd(var1), var2 = sd(var2), var3 = sd(var3)) 

table2 = melt(m_mean) 
table3 = melt(m_devs) 

colnames(table2)[3] = "Mean" 
colnames(table3)[3] = "SD" 

table4 = merge(table2, table3) 

ggplot(table4, aes(group, Mean, fill = variable)) + 
geom_bar(stat = "identity") + 
geom_errorbar(aes(ymax = Mean + SD, ymin=Mean - SD), position = "identity", width = 1) 

这里是一个类似的曲线使用随机数据:

enter image description here

这里是一个解决方案,绘制之前计算y位置:

# solution 
table4$y_pos = NA 
table4$y_pos[table4$variable == "var1"] = table4$Mean[table4$variable == "var1"] 
table4$y_pos[table4$variable == "var2"] = table4$Mean[table4$variable == "var1"] + 
table4$Mean[table4$variable == "var2"] 
table4$y_pos[table4$variable == "var3"] = table4$y_pos[table4$variable == "var2"] + 
table4$Mean[table4$variable == "var3"] 

ggplot(table4, aes(group, Mean, fill = variable)) + 
geom_bar(stat = "identity") + 
geom_errorbar(aes(ymax = y_pos + SD, ymin = y_pos - SD), position = "identity", width = 1) 

enter image description here