2015-09-10 138 views
6

我想立刻使用dygraph繪製多條曲線(它們不具有在第一步驟中被同步)dygraph R中多條曲線一次

基地R-例如:

temperature <- ts(frequency = 12, start = c(1980, 1), 
     data = c(7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 
     25.2, 26.5, 23.3, 18.3, 13.9, 9.6)) 
rainfall <- ts(frequency = 12, start = c(1980, 1), 
     data = c(49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 
     135.6, 148.5, 216.4, 194.1, 95.6, 54.4)) 

par(mfrow = c(2, 1)) 
plot(temperature) 
plot(rainfall) 

隨着dygraph這種方法行不通

require(dygraphs) 
par(mfrow = c(2, 1)) 
dygraph(temperature) 
dygraph(rainfall) 

我知道有顯示第二軸等的可能性,但也許有人知道一個答案,在一次

0123顯示兩個圖

回答

1

要繪製多個dygraphs在同一RStudio窗口中,您必須首先創建的dygraphs對象名單,然後使用包htmltools呈現dygraphs列表。 RStudio的Yihui Xie在這裏提供了答案: Yihui Xie answer(但沒有分組)。
我在這裏回答了一個類似的問題:my answer

這裏正在R代碼產生分組情況(同步)dygraphs圖:

# create the time series 
temperature <- ts(frequency = 12, start = c(1980, 1), 
       data = c(7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 
         25.2, 26.5, 23.3, 18.3, 13.9, 9.6)) 
rainfall <- ts(frequency = 12, start = c(1980, 1), 
      data = c(49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 
        135.6, 148.5, 216.4, 194.1, 95.6, 54.4)) 

# create a list of dygraphs objects 
library(dygraphs) 
library(htmltools) 
dy_graph <- list(
    dygraphs::dygraph(temperature, group="temp_rain", main="temperature"), 
    dygraphs::dygraph(rainfall, group="temp_rain", main="rainfall") 
) # end list 

# render the dygraphs objects using htmltools 
htmltools::browsable(htmltools::tagList(dy_graph)) 

上面R代碼產生以下組合(同步)dygraphs圖:

enter image description here