2013-03-11 29 views
1

我正在繪製不同氣候模式的條形圖。我想做一個比較模型和觀察的情節。氣候模型將繪製成bar(geom_bar()),但我想要觀察橫條。使用橫槓ggplot2

下面的腳本繪製了一個圖,但是在圖上方有一些東西(顛倒的traingle)。這個腳本有什麼問題?我錯過了什麼嗎?

ch<-structure(list(Month = structure(c(4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, 1L, 2L, 3L), .Label = c("Oct", "Nov", "Dec", "Jan", 
"Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep"), class = c("ordered", 
"factor")), GCM1 = c(169.5, 157.19, 90.07, 42.97, 13.24, 1.56, 
2.53, 5.99, 14.92, 46.35, 88.23, 138.02), GCM2 = c(215.01, 193.37, 
131.14, 41.48, 7.63, 0.94, 0.81, 0.78, 1.88, 15.95, 99.58, 188.16 
), GCM3 = c(164.83, 158.82, 97.5, 29.27, 5.47, 2.14, 3.34, 0.85, 
9.94, 16.9, 57.21, 117.05), OBS = c(142.25, 138.59, 59.95, 26.48, 
2.61, 0.2, 0.1, 0.4, 0.72, 11.64, 38.75, 119.82)), .Names = c("Month", 
"GCM1", "GCM2", "GCM3", "OBS"), row.names = c(NA, -12L), class = "data.frame") 

ch$Month<-month.abb 
ch$Month<-factor(ch$Month, levels=c(month.abb[10:12],month.abb[1:9]), ordered=TRUE) 

chm<-melt(ch, id="Month") 

cbPalette1 <- cbbPalette <- c("#D55E00", "#56B4E9", "#009E73","#0072B2", "#CC79A7","#000000") 

p<-ggplot(data=chm,aes(x=factor(Month),y=value,group=variable,fill=variable))+geom_bar(subset = .(variable != "OBS"),stat="identity",position=position_dodge())+ 
       scale_fill_manual(values=cbPalette1)+ 
       geom_crossbar(subset = .(variable == "OBS"),aes(ymin = min(value), ymax = max(value)), col="gray30",fatten=3) 

.......

提前感謝

BHH

+1

你的問題是不可再現的,因爲'OBS'對象丟失。 – juba 2013-03-11 13:33:18

+0

這是不可複製的,但我不確定這是否是原因。 ChBS有一個OBS專欄。 – Arun 2013-03-11 13:37:13

+0

juba&Arun,我已經刪除了「data = OBS」部分。這是一個糾正這個問題的試驗。 – Hamz 2013-03-11 20:49:52

回答

1

兩件事情:

  1. 要覆蓋羣審美只是variable,所以 在crossbar中,它忽略了不同的x的值(將其視爲連續處理 ),這是一個奇怪的交叉開關。

  2. 我想你只是想要酒吧本身,而不是圍繞它的任何程度。 如果是這樣,您想將yminymax設置爲中心值,而不是 到所有中心值​​的範圍。

使這兩個變化:

p<-ggplot(data=chm, 
      aes(x = Month, 
       y = value, 
       fill = variable)) + 
    geom_bar(subset = .(variable != "OBS"), 
      stat="identity", 
      position=position_dodge()) + 
    scale_fill_manual(values=cbPalette1)+ 
    geom_crossbar(subset = .(variable == "OBS"), 
       aes(ymin = value, ymax = value), 
       col="gray30", fatten=3) 

enter image description here

+0

謝謝Brian,這是我想達到的。 – Hamz 2013-03-12 11:34:39