2014-10-08 18 views
8

我不知道這種類型的情節的名稱(歡迎發表評論)。從本質上講,它是一個帶有字形的巴洛圖,填充後表示損失/收益。字形是箭頭,像編碼有關方向,大小的信息,並允許查看條形幾何。差異圖

enter image description here

這看起來很有趣,但也想不出如何做到這一點的ggplot2(網格框架工作)。我們如何在ggplot2/grid框架中重新創建這種情節(基礎解決方案對於問題的完整性也很受歡迎)。特別是glyphs,而不是文本,因爲這在ggplot2中已經非常簡單。

這裏有一些代碼來創建數據和傳統的覆蓋&座標翻轉躲閃條形圖和線圖,以顯示這種類型的數據可視化的典型方法。

set.seed(10) 
x <- sample(30:60, 12) 
y <- jitter(x, 60) 

library(ggplot2) 
dat <- data.frame(
    year = rep(2012:2013, each=12), 
    month = rep(month.abb, 2), 
    profit = c(x, y) 
) 


ggplot() + 
geom_bar(data=subset(dat, year==2012), aes(x=month, weight=profit)) + 
geom_bar(data=subset(dat, year==2013), aes(x=month, weight=profit), width=.5, fill="red") 

ggplot(dat, aes(x=month, fill=factor(year))) + 
    geom_bar(position="dodge", aes(weight=profit)) + 
    coord_flip 

ggplot(dat, aes(x=month, y=profit, group = year, color=factor(year))) + 
    geom_line(size=1) 

enter image description here

enter image description here

enter image description here

+0

''annotation_custom'''polygonGrob''s可能工作。 – hrbrmstr 2014-10-08 03:40:34

+4

你的例子比原始圖更好。這些字形充其量是令人困惑的,最糟糕的是誤導。 – thelatemail 2014-10-08 04:06:59

+0

像@thelatemail說的!通常不推薦在一張圖上繪製兩組完全不同的數據(值和增量),因爲它很難破譯。 – 2014-10-08 11:40:49

回答

11

下面是一個例子,也許還有其他的方式,雖然,

dat <- data.frame(
    year = rep(2012:2013, each=12), 
    month = factor(rep(1:12, 2), labels=month.abb), 
    profit = c(x, y) 
) 
dat2 <- reshape2::dcast(dat, month~ year, value.var = "profit") 
names(dat2)[2:3] <- paste0("Y", names(dat2)[2:3]) 

ggplot(dat2) + 
    geom_bar(aes(x=month, y = Y2012), stat = "identity", fill = "grey80", width = 0.6) + 
    geom_segment(aes(x=as.numeric(month)-0.4, xend = as.numeric(month)+0.4, y = Y2013, yend = Y2013)) + 
    geom_segment(aes(x = month, xend = month, y = Y2013, yend = Y2012, colour = Y2013 < Y2012), 
       arrow = arrow(60, type = "closed", length = unit(0.1, "inches")), size = 1.5) + 
    theme_bw() 

enter image description here

+0

完美。 +1我同意@thelatemail這可能不是最好的方法,如果尋找更大的模式。我認爲折線圖會做得更好。但是這裏的圖形實際上改進了我演示的設計,箭頭覆蓋了酒吧的幾何圖形。如果有人希望逐月比較更準確,這種方法可能會有一些好處。我知道沒有任何經驗研究比較了這類圖表對於這樣的任務的感知,但是我可以看到它可以有一個優點,允許人們快速確定月份的變化和幅度,而不會丟失關於整體利潤的信息。 – 2014-10-08 12:23:50

+0

一個人可以專注於彩色箭頭或注意條形或注意差異線(不知道這些技術名稱,但是第2年水平線),而不會被競爭性的預注意屬性過分分心。圖表質量和設計是關於預期的目的。我可以看出,這種圖表類型在適當的情況下可能有優點。感謝您的方法。 – 2014-10-08 12:24:26