2015-04-16 19 views
0

我的數據看起來是這樣的:ggplot order by discrete mean by y?

df1 <- structure(list(truth = structure(c(1L, 60L, 60L, 1L, 60L, 1L, 
              51L, 60L, 1L, 51L, 51L, 51L, 1L, 60L, 1L, 1L, 1L, 1L, 51L, 51L, 
              60L, 1L, 51L, 60L, 60L, 51L, 1L, 51L, 51L, 60L, 1L, 51L, 60L, 
              60L, 60L, 60L, 51L, 51L, 1L, 51L, 60L, 60L, 51L, 60L, 51L, 1L, 
              60L, 60L, 1L, 51L), .Label = c("1", "2", "3", "4", "5", "6", 
                     "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", 
                     "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", 
                     "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", 
                     "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", 
                     "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", 
                     "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", 
                     "73", "74", "75", "76", "77", "78", "79", "80", "81"), class = "factor"), 
         Value = c(-30.0770205345024, 11.7433058396476, 7.36998070970907, 
           -40.9432745219622, 11.2711232296384, 37.654418952948, 52.0790876912742, 
           9.24338996112739, 29.0493670430368, 49.1051619881392, 44.2985212410612, 
           43.6318989421322, 65.4604885768842, 8.53340577821466, 28.0911627592635, 
           -10.9056308891468, -30.7565582432717, -10.6123772579921, 
           72.062213840367, 48.7134304917154, 5.27055421605771, 84.0339742464238, 
           44.9513792584612, 8.69314684177602, 4.46763929211751, 67.201960023123, 
           18.8362161729668, 55.8315346585296, 42.795252431141, 10.6963690858218, 
           20.4073049396301, 72.4095655954934, 6.57602161344761, 11.3705463495041, 
           5.75891202331646, 4.4898446961011, 61.9778344010139, 45.713765588086, 
           -50.570124971873, 65.3016561445738, 13.413626538152, 5.37048273610232, 
           59.9860827960121, 12.1028140616031, 56.3708005716536, -29.5009161157071, 
           7.22681794770881, 4.27798086289943, -20.8395270111429, 57.8745694222055 
        )), .Names = c("truth", "Value"), row.names = c(88L, 126L, 
                     288L, 232L, 78L, 76L, 83L, 237L, 52L, 62L, 275L, 194L, 103L, 
                     117L, 262L, 184L, 25L, 205L, 119L, 203L, 15L, 64L, 74L, 225L, 
                     297L, 176L, 277L, 197L, 5L, 105L, 7L, 38L, 12L, 285L, 63L, 201L, 
                     56L, 155L, 247L, 128L, 240L, 72L, 44L, 51L, 29L, 82L, 249L, 57L, 
                     112L, 8L), class = "data.frame") 

我想產生這樣

df1 %>% ggplot(aes(x=truth, y=Value)) + 
    stat_summary(fun.data=mean_cl_boot, mult=1, 
       geom="pointrange", conf.int=0.95) + 
    theme_bw() 

一個情節,我要重新排序「真相」 acording爲每一個平均值。在這個例子中,1,60,51。

我該怎麼做?

謝謝!

+1

見http://stackoverflow.com/questions/5738619/ggplot2-possible-to-reorder-xs-by-value-of-computed-y-stat-summary – bergant

回答

2

您可以重這樣

ggplot(data = df1, aes(reorder(truth, truth),Value)) + 
stat_summary(fun.data=mean_cl_boot, mult=1, 
geom="pointrange", conf.int=0.95) + 
theme_bw() 

編輯


如果要重新排序基於值列,然後做這個

ggplot(data = df1, aes(reorder(truth, Value),Value)) + 
stat_summary(fun.data=mean_cl_boot, mult=1, 
geom="pointrange", conf.int=0.95) + 
theme_bw() 

而且如果您想要的任何情況下根據價值列的降序重新排序,你可以這樣做:)

ggplot(data = df1, aes(reorder(truth, -Value),Value)) 
+0

我嘗試運行,而不是使任何區別:( – Ignacio

+0

是的。我認爲Veerandra有一個錯字。它應該是aes(reorder(真相,價值),價值) – Ignacio