2015-01-15 43 views
0

我的數據是一個table()關於四個變量(數據如下)的輸出。帶有方面和組填充的barplot中的訂單因子變量

我想繪製每個V3的N,用V1填充並且用V2刻面。但之前,V1應由V4訂購。

temp$V1 <- reorder(temp$V1, temp$V4) 
ggplot(temp, aes(x=V3, y=N, fill=V1)) + 
    geom_bar(binwidth = 10, stat="identity") + 
    ylab("") + xlab("") + 
    facet_grid(. ~ V2) + 
    scale_fill_discrete(name = "Journal") + 
    theme(legend.title = element_text(colour="black", size=14), 
     legend.direction="horizontal", 
     legend.position="bottom", 
     strip.text.x = element_text(size = 14), 
     axis.text = element_text(colour="black", size=10), 
     axis.ticks.x = element_blank(), 
     panel.grid.minor.x=element_blank(), 
     panel.grid.major.x=element_blank() 
) 

但酒吧元素仍然沒有按照我的輸出我想要的規則有序的樣子

enter image description here

的順序應該是JBF,JFI,JMCB,JF,JFE,RFS。我究竟做錯了什麼?有太多的維度嗎?

這裏是數據:

V1 V2 V3  V4 N 
    JBF 1998 with 0 97 
    JFI 1998 with 0 21 
    JMCB 1998 with 0 62 
    JBF 1999 with 0 93 
    JFI 1999 with 0 20 
    JMCB 1999 with 0 37 
    JBF 2000 with 0 112 
    JFI 2000 with 0 29 
    JMCB 2000 with 0 65 
    JBF 2009 with 0 247 
    JFI 2009 with 0 40 
    JMCB 2009 with 0 126 
    JBF 2010 with 0 299 
    JFI 2010 with 0 27 
    JMCB 2010 with 0 110 
    JBF 2011 with 0 311 
    JFI 2011 with 0 34 
    JMCB 2011 with 0 120 
    JBF 1998 without 0 29 
    JMCB 1998 without 0 9 
    JBF 1999 without 0 62 
    JMCB 1999 without 0 16 
    JBF 2000 without 0 37 
    JFI 2000 without 0 2 
    JMCB 2000 without 0 20 
    JBF 2009 without 0 158 
    JFI 2009 without 0 2 
    JMCB 2009 without 0 23 
    JBF 2010 without 0 153 
    JFI 2010 without 0 2 
    JMCB 2010 without 0 28 
    JBF 2011 without 0 133 
    JFI 2011 without 0 1 
    JMCB 2011 without 0 12 
    JF 1998 with 1 133 
    JFE 1998 with 1 91 
    RFS 1998 with 1 44 
    JF 1999 with 1 134 
    JFE 1999 with 1 101 
    RFS 1999 with 1 72 
    JF 2000 with 1 147 
    JFE 2000 with 1 101 
    RFS 2000 with 1 74 
    JF 2009 with 1 154 
    JFE 2009 with 1 181 
    RFS 2009 with 1 284 
    JF 2010 with 1 128 
    JFE 2010 with 1 179 
    RFS 2010 with 1 218 
    JF 2011 with 1 103 
    JFE 2011 with 1 249 
    RFS 2011 with 1 189 
    JF 1998 without 1 12 
    JFE 1998 without 1 2 
    JF 1999 without 1 18 
    JFE 1999 without 1 2 
    RFS 1999 without 1 3 
    JF 2000 without 1 8 
    JFE 2000 without 1 6 
    RFS 2000 without 1 2 
    JF 2009 without 1 6 
    JFE 2009 without 1 4 
    RFS 2009 without 1 4 
    JFE 2010 without 1 9 
    RFS 2010 without 1 8 
    JF 2011 without 1 3 
    JFE 2011 without 1 7 
    RFS 2011 without 1 9 

編輯:的dput(temp)

structure(list(V1 = structure(c(3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 
1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 1L, 3L, 1L, 3L, 2L, 
1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 6L, 4L, 5L, 6L, 4L, 5L, 
6L, 4L, 5L, 6L, 4L, 5L, 6L, 4L, 5L, 6L, 4L, 5L, 6L, 4L, 6L, 4L, 
5L, 6L, 4L, 5L, 6L, 4L, 5L, 4L, 5L, 6L, 4L, 5L), .Label = c("JMCB", 
"JFI", "JBF", "JFE", "RFS", "JF"), class = c("ordered", "factor" 
)), V2 = c("1998", "1998", "1998", "1999", "1999", "1999", "2000", 
"2000", "2000", "2009", "2009", "2009", "2010", "2010", "2010", 
"2011", "2011", "2011", "1998", "1998", "1999", "1999", "2000", 
"2000", "2000", "2009", "2009", "2009", "2010", "2010", "2010", 
"2011", "2011", "2011", "1998", "1998", "1998", "1999", "1999", 
"1999", "2000", "2000", "2000", "2009", "2009", "2009", "2010", 
"2010", "2010", "2011", "2011", "2011", "1998", "1998", "1999", 
"1999", "1999", "2000", "2000", "2000", "2009", "2009", "2009", 
"2010", "2010", "2011", "2011", "2011"), V3 = c("with", "with", 
"with", "with", "with", "with", "with", "with", "with", "with", 
"with", "with", "with", "with", "with", "with", "with", "with", 
"without", "without", "without", "without", "without", "without", 
"without", "without", "without", "without", "without", "without", 
"without", "without", "without", "without", "with", "with", "with", 
"with", "with", "with", "with", "with", "with", "with", "with", 
"with", "with", "with", "with", "with", "with", "with", "without", 
"without", "without", "without", "without", "without", "without", 
"without", "without", "without", "without", "without", "without", 
"without", "without", "without"), V4 = c("0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1"), 
    N = c(97L, 21L, 62L, 93L, 20L, 37L, 112L, 29L, 65L, 247L, 
    40L, 126L, 299L, 27L, 110L, 311L, 34L, 120L, 29L, 9L, 62L, 
    16L, 37L, 2L, 20L, 158L, 2L, 23L, 153L, 2L, 28L, 133L, 1L, 
    12L, 133L, 91L, 44L, 134L, 101L, 72L, 147L, 101L, 74L, 154L, 
    181L, 284L, 128L, 179L, 218L, 103L, 249L, 189L, 12L, 2L, 
    18L, 2L, 3L, 8L, 6L, 2L, 6L, 4L, 4L, 9L, 8L, 3L, 7L, 9L)), .Names = c("V1", 
"V2", "V3", "V4", "N"), class = c("data.table", "data.frame"), row.names = c(NA, 
-68L), .internal.selfref = <pointer: 0x318e998>) 
+0

看來你想重新排序這樣的因素; 'temp $ V1 < - factor(temp $ V1,levels = c(「JBF」,「JFI」,「JMCB」,「JF」,「JFE」,「RFS」))。 – jazzurro

+1

我無法重現此問題:當我在表格中讀取並運行代碼時,標籤的排列順序與您想要的完全相同。你能提供'dput(temp)'的結果嗎? –

+0

你應該寫'ordered = TRUE'以及@ jazzurro的回答,比如'temp $ V1 < - factor(temp $ V1,levels = c(「JBF」,「JFI」,「JMCB」,「JF」,「JFE 「,」RFS「),ordered = T)' – Koundy

回答

0

唯一命令輸出缺是繪製之前重新定義temp

temp <- temp[order(temp$V1), ] 

因爲在THI在這種情況下,temp確實是有序的,而不僅僅是虛擬的。