2014-12-04 104 views
3

我正在嘗試使用R的gglot2軟件包中的條形圖將基準值的時間序列偏移量可視化。例如,採取以下綜合數據:通過條形圖顯示與目標值的差異

baseline = 400 
steps <- sample(0:10,50,replace=TRUE) - sample(0:10,50,replace=TRUE) 
value <- cumsum(steps) + baseline 
time = 1:50 
data <- data.frame(time,value) 

print(value) 
[1] 400 400 397 397 393 400 394 395 389 389 385 395 400 399 405 403 399 401 399 401 
[21] 401 401 398 397 395 395 401 402 393 400 399 398 406 412 417 413 410 401 400 399 
[41] 394 401 406 406 401 404 411 413 404 402 

我可以繪製圖表在其原規模,但這不是真正的信息:

longdata <- ddply(data, "value", transform, posneg=sign(value-baseline)) 
longdata[longdata$posneg == 0,'posneg'] <- 1 
p_aes <- aes(time, value, fill=factor(posneg)) 
p_scale <- scale_fill_brewer(palette='Set1', guide=FALSE) 
p_geom <- geom_bar(stat='identity', position='identity') 
ggplot(longdata) + p_aes + p_scale + p_geom 

enter image description here

通過沿着換擋審美y軸(即,y =值 - 基線)我得到我想要顯示的圖表,這很好,很容易。

longdata <- ddply(data, "value", transform, posneg=sign(value-baseline)) 
longdata[longdata$posneg == 0,'posneg'] <- 1 
p_aes <- aes(time, value-baseline, fill=factor(posneg)) 
p_scale <- scale_fill_brewer(palette='Set1', guide=FALSE) 
p_geom <- geom_bar(stat='identity', position='identity') 
ggplot(longdata) + p_aes + p_scale + p_geom 

enter image description here

不幸的是,在y軸的刻度現在被改變爲從基線的偏移量,即,「值 - 基線」。但是,我確實希望y軸保持原始值(380和420之間的值)。

有什麼辦法可以保留第二張圖的原始y軸刻度嗎?您是否有任何其他建議可視化與目標值的差異?

回答

5

添加功能:

yaxis_format <- function(x){ 
lab <- 400-x 
} 

,然後用scale_y_continuous(label = yaxis_format)處理標籤:

ggplot(longdata) + p_aes + p_scale + p_geom + scale_y_continuous(label=yaxis_format) 

最後的代碼和圖形應該是這樣的:

library(ggplot2) 
library(plyr) 

set.seed(201) 

baseline = 400 
steps <- sample(0:10,50,replace=TRUE) - sample(0:10,50,replace=TRUE) 
value <- cumsum(steps) + baseline 
time = 1:50 
data <- data.frame(time,value) 

yaxis_format <- function(x){ 
lab <- 400-x 
} 

longdata <- ddply(data, "value", transform, posneg=sign(value-baseline)) 
longdata[longdata$posneg == 0,'posneg'] <- 1 
p_aes <- aes(time, value-baseline, fill=factor(posneg)) 
p_scale <- scale_fill_brewer(palette='Set1', guide=FALSE) 
p_geom <- geom_bar(stat='identity', position='identity') 
ggplot(longdata) + p_aes + p_scale + p_geom + scale_y_continuous(label=yaxis_format) 
       + ylab("Value") 

enter image description here

現在,隨着這一切的設置,請注意,規模很奇怪。使用scale_y_reverse,而不是修復它:

ggplot(longdata) + p_aes + p_scale + p_geom + scale_y_reverse(label=yaxis_format) 
       + ylab("Value") 

enter image description here

+0

啊,我只是做了回答,好。國際海事組織可以留在這裏,只是談論美學和潛在的選擇(例如,使它比普通代碼更通用一些)。 – 2014-12-04 13:59:59

4

另一種解決方案,而不是-事後改變y軸是用geom_linerange,然後只是使線足夠寬的特定情節(不同geoms對於交叉或錯誤條也可能適用)。

p <- ggplot(data=longdata, aes(x = time, color = factor(posneg))) + 
    geom_linerange(aes(ymax = value, ymin = baseline), size = 3) + 
    scale_color_brewer(palette='Set1', guide=FALSE) 

p 

enter image description here

這是一個完全合理的情節使,因爲你表明你被縮小,並使用基線在零規則錯過所有的變化。但是,條形圖具有如此強大的約定,即基線應該爲零,可能會被誤讀。此外,基線值處的條紋根本不會出現在圖中,這使得它們看起來像缺少數據。

用水平條代表基線的線條圖足以顯示相同的信息,實際上不需要顏色。

p2 <- ggplot(data=longdata, aes(x = time, y = value)) + 
     geom_line() + geom_point() + geom_hline(yintercept=baseline) 

p2 

enter image description here

+0

謝謝,這是聲音的建議,我會考慮一個線條圖。 – MRA 2014-12-08 16:05:08