2011-02-18 107 views

回答

2

的基本思想是融化的數據集,讓你有你要在一個列在y軸上繪製的變量的值,用第二列區分來源。例如:

data("economics") 
dat.m <- melt(economics, measure.vars=c("pop", "unemploy")) 

然後使用facet_grid繪製每個變量在一個單獨的方面:

ggplot(dat.m, aes(x=date, y=value)) + geom_line() + facet_grid(variable~., scales="free_y") 
1

雅虎財經圖表通常是互動的,在那裏你可以移動的時間窗口等多種功能。要獲得類似的互動效果,我會推薦googleVis包。這些圖是通過網絡瀏覽器製作的,因此您可以與它們進行交互。嘗試:

install.packages("googleVis");library(googleVis);demo(googleVis)

第8圖表彈出的是一個時間序列。 但這不是ggplot。

0

Ista使用ggplot2的戰略大綱也可以使用lattice包來實現。使用相同的數據:

data("economics") 
dat.m <- melt(economics, measure.vars=c("pop", "unemploy")) 

然後我們使用xyplot,經由layout指定兩行和一列並使用scales參數強迫分開y軸的刻度:

xyplot(value~date|variable,data = dat.m, 
    panel = "panel.lines", layout = c(1,2), 
    scales = list(y = list(relation = "free"))) 

enter image description here

6

使用其他人提到一個很好的baseR解決方案可能是經濟數據集。

layout(matrix(1:2, ncol = 1), widths = 1, heights = c(2,1.5), respect = FALSE) 
par(mar = c(0, 4.1, 4.1, 2.1)) 
with(economics, plot(unemploy~date, type = 'l', xaxt = 'n', main = 'My Great Graph')) 
par(mar = c(4.1, 4.1, 0, 2.1)) 
with(economics, plot(pop~date, type = 'l')) 

my great graph

你會注意到,你並不需要重塑在所有的,因爲你明確做兩個圖表中的數據,而不必使用其中數據是在因子分解函數。另外,這兩個圖可以是幾乎任何東西......就像第二個圖可以是直方圖。另外,你會注意到,在這段代碼中,你可以將相對高度設置爲任何你想要的。 (這段代碼比ggplot2解決方案執行速度要快得多...萬一有什麼問題...比如實時更新網站或其他東西,或者你有很多數據,或者需要製作許多這樣的圖表)

相關問題