2016-12-04 59 views
0

有沒有辦法爲ggplot設置寬度?幾個ggplots與grid.arrange函數結合使用的相同寬度

我想在一列中合併三個時間曲線。 由於y軸值,地塊具有不同的寬度(兩個地塊的座標軸值在範圍(-20,50),一個(18000,25000) - 這使得情節變得簡單)。 我想讓所有的地塊完全一樣的寬度。

plot1<-ggplot(DATA1, aes(x=Date,y=Load))+ 
    geom_line()+ 
    ylab("Load [MWh]") + 
    scale_x_date(labels = date_format("%m/%y"),breaks = date_breaks("months"))+ 
    theme_minimal()+ 
    theme(panel.background=element_rect(fill = "white")) 
plot2<-ggplot(DATA1, aes(x=Date,y=Temperature))+ 
    geom_line()+ 
    ylab("Temperature [C]") + 
    scale_x_date(labels = date_format("%m/%y"),breaks = date_breaks("months"))+ 
    theme_minimal()+ 
    theme(panel.background=element_rect(fill = "white")) 
plot3<-ggplot(DATA1, aes(x=Date,y=WindSpeed))+ 
    geom_line()+ 
    ylab("Wind Speed [km/h]") + 
    scale_x_date(labels = date_format("%m/%y"),breaks = date_breaks("months"))+ 
    theme_minimal()+ 
    theme(panel.background=element_rect(fill = "white")) 
grid.arrange(plot1, plot2, plot3, nrow=3) 

結合劇情是這樣的: enter image description here

+0

我喜歡'?cowplot :: plot_grid'比'grid arrange'更好,因爲這樣的東西,可能值得一看 – Nate

+0

@NathanDay謝謝:)我會研究它。 – ppi0trek

回答

1

您可以簡單地使用磨製這一點。首先,你必須做一些數據的改寫(munging):

library(tidyr) 

new_data = gather(DATA1, variable, value, Load, Temperature, WindSpeed) 

LoadTemperatureWindspeed所有的數據收集到一個大列(value)。另外,還會創建一個額外的列(variable),它指定矢量中的哪個值屬於哪個變量。

之後,你可以繪製數據:

ggplot(new_data) + geom_line(aes(x = Date, y = value)) + 
    facet_wrap(~ variable, scales = 'free_y', ncol = 1) 

現在GGPLOT2會照顧所有繁重的。

ps如果你讓你質疑reproducible,我可以讓我的答案重現。

+0

非常感謝!這正是我一直在尋找的:)通過讓我的問題可重複的,你的意思是添加我的數據集? – ppi0trek

+0

你應該知道這個答案中的方法沒有考慮到變量有不同的單位。您必須單獨設置y軸標籤,以使用'註釋' –

+0

@JakeKaupp將差異合併到單位中,謝謝我使用'gsub'來更改其中包含單位的變量名稱。 (new_data $ variable < - gsub(「Load」,「Load MWh」,new_data $ variable)它運行良好:) – ppi0trek

相關問題