2016-09-27 3 views
0

Ich habe versucht, die Fehlerbalken auf meinen gestapelten Balken zu zeichnen.Fehlerbalken auf Stack-Balken ggplot2 R

Ich habe zwei Datenmatrix, eine ist der Mittelwert der und der andere ist der Standard-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 

Ich änderte sowohl das Format Matrix aus WIDE zu 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) 

habe ich die Grafik gezeichnet mit 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)) 

Ich habe versucht, die Fehlerbalken folgenden einige nützliche zu ziehen Antworten zum Beispiel: https://stackoverflow.com/search?q=stacked+bars+errors+ggplot aber ich unterstelle nicht nd warum ich nicht die Fehlerbalken mit dem richtigen Wert ziehen und die richtigen Position

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 

Dies ist das Grundstück

enter image description here

+0

Fehlerbalken in Ordnung (gute y-Position), aber Ihre Stäbe werden gestapelt aussehen. Brauchen Sie wirklich Stacker Bars? Wenn Sie sie benötigen, können Sie neue y-Werte (Mittelwert) für jede Fehlerleiste berechnen, z. TP 'y sollte (Mean TP') + (Mean TP *) anstelle von (Mean TP *) sein. – bVa

+0

Ich muss die drei Parameter (TP ', TP * und FP) als gestapelte Balken anzeigen. Ich versuche, die Fehlerbalken für jeden Balken zu zeichnen, also 3 Fehlerbalken pro Spalte. –

Antwort

0

Als ich bemerkte, können Sie direkt neue y_position für jede errorbar berechnen :

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) 

Hier ist eine ähnliche Darstellung mit Zufallsdaten:

enter image description here

Und hier ist eine Lösung, y Position Berechnung vor dem Plotten:

# 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