2017-03-13 86 views
0

我想繪製在R使用ggplot2在酒吧上的誤差酒吧。酒吧的位置是好的,但錯誤酒吧錯位,重疊。請看看下面的例子:ggplot2 - 酒吧與錯誤酒吧不對齊

library(ggplot2) 

df = structure(list(variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 
4L, 4L), .Label = c("Stage1", "Stage2", "Stage3", "Stage4"), class = "factor"), 
    scenario = c("A", "A", "A", "B", "B", 
    "B", "A", "A", "A", "B", "B", 
    "B", "A", "A", "A", "B", "B", 
    "B", "A", "A", "A", "B", "B", 
    "B"), Period = c("2011-2040", "2041-2070", "2071-2100", 
    "2011-2040", "2041-2070", "2071-2100", "2011-2040", "2041-2070", 
    "2071-2100", "2011-2040", "2041-2070", "2071-2100", "2011-2040", 
    "2041-2070", "2071-2100", "2011-2040", "2041-2070", "2071-2100", 
    "2011-2040", "2041-2070", "2071-2100", "2011-2040", "2041-2070", 
    "2071-2100"), value = c(-1, -2, -3, -1, -3, -4, -2, -1, -1, 
    -2, -1, -1, -5, -6, -6, -5, -6, -7, -1, -1, -1, -1, -1, -1 
    ), error = c(0.861817443828852, 0.948522930371594, 0.916295430216828, 
    1.09513465405609, 0.967022228830058, 0.962082007787887, 0.327433241996297, 
    0.296270630257992, 0.241564623986049, 0.324799025797241, 
    0.287300168855865, 0.321141699334506, 0.448402583575847, 
    0.329720917851603, 0.296149789779728, 0.423049544646902, 
    0.320807147599275, 0.286926161100617, 0.46555636533774, 0.494769057825624, 
    0.498445009296978, 0.490846868961772, 0.478102139172148, 
    0.47821442683311), ymin = c(-1.86181744382885, -2.94852293037159, 
    -3.91629543021683, -2.09513465405609, -3.96702222883006, 
    -4.96208200778789, -2.3274332419963, -1.29627063025799, -1.24156462398605, 
    -2.32479902579724, -1.28730016885586, -1.32114169933451, 
    -5.44840258357585, -6.3297209178516, -6.29614978977973, -5.4230495446469, 
    -6.32080714759928, -7.28692616110062, -1.46555636533774, 
    -1.49476905782562, -1.49844500929698, -1.49084686896177, 
    -1.47810213917215, -1.47821442683311), ymax = c(-0.138182556171148, 
    -1.05147706962841, -2.08370456978317, 0.0951346540560851, 
    -2.03297777116994, -3.03791799221211, -1.6725667580037, -0.703729369742008, 
    -0.758435376013951, -1.67520097420276, -0.712699831144135, 
    -0.678858300665494, -4.55159741642415, -5.6702790821484, 
    -5.70385021022027, -4.5769504553531, -5.67919285240072, -6.71307383889938, 
    -0.53444363466226, -0.505230942174376, -0.501554990703022, 
    -0.509153131038228, -0.521897860827852, -0.52178557316689 
    )), .Names = c("variable", "scenario", "Period", "value", 
"error", "ymin", "ymax"), class = "data.frame", row.names = c(NA, 
-24L)) 


ggplot(data=df, aes(x=Period, y=value)) + 
    geom_bar(aes(fill=variable), position='dodge', stat="identity", width=0.75) + 
    geom_errorbar(aes(ymin=ymin, ymax=ymax, width=.2)) + 
    facet_wrap(~scenario) + 
    theme_bw(base_size=16) 

enter image description here

正如你所看到的,在誤差不正確的數據條邊對齊。我也嘗試了一些在網上找到的解決方案,例如:

geom_errorbar(aes(ymin=ymin, ymax=ymax), position = position_dodge(0.9)) 

但無濟於事。

我的ggplot命令有什麼問題?

回答

2

您應該在ggplot()aes()裏添加group = variable以使position_dodge()正常工作。

ggplot(data=df, aes(x=Period, y=value, group = variable)) + 
     geom_bar(aes(fill=variable), position='dodge', stat="identity", width=0.75) + 
     geom_errorbar(aes(ymin=ymin, ymax=ymax), position = position_dodge(0.75),width = 0.2) + 
     facet_wrap(~scenario) + 
     theme_bw(base_size=16) 

同樣可以,如果你從geom_bar()移動fill = variableggplot()aes()被atchieved。

ggplot(data=df, aes(x=Period, y=value, fill = variable)) + 
     geom_bar(position='dodge', stat="identity", width=0.75) + 
     geom_errorbar(aes(ymin=ymin, ymax=ymax), position = position_dodge(0.75),width = 0.2) + 
     facet_wrap(~scenario) + 
     theme_bw(base_size=16) 
+0

這兩個解決方案的工作,謝謝! – thiagoveloso