2016-12-04 8 views
0

ggplot의 너비를 설정하는 방법이 있습니까?grid.arrange 함수와 결합 된 몇 개의 ggplots와 동일한 너비

하나의 열에 세 개의 시간 표시를 결합하려고합니다. y 축 값 때문에 플롯의 폭이 다릅니다 (두 개의 플롯은 범위 (-20, 50), 하나 (18 000, 25 000) - 플롯을 더 얇게 만듭니다). 모든 플롯을 정확히 같은 폭으로 만들고 싶습니다.

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) 

결합 줄거리는 다음과 같습니다 : 당신은 단순히 이것에 대한 facetting을 사용할 수 있습니다 enter image description here

+0

나는'? cowplot :: plot_grid'가'grid arrangement'보다 좋기 때문에 이와 같은 것들 때문에, 보일 가치가 있을지도 모른다. – Nate

+0

@ NathanDay thanks :) 나는 그것에 대해 조사 할 것이다. – ppi0trek

답변

1

. 먼저 일부 데이터 munging을 할 필요가 :

library(tidyr) 

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

이 하나의 큰 열 (value)에 Load, TemperatureWindspeed의 모든 데이터를 수집합니다. 또한 벡터의 어느 값이 어떤 변수에 속하는지를 지정하는 추가 열이 생성됩니다 (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 축 레이블을 설정하여 'annotate' –

+0

@ JakeKaupp을 사용하여 단위의 차이를 통합해야합니다. 덕분에 'gsub'을 사용하여 단위가있는 변수 이름을 변경했습니다. (new_data $ variable <- gsub ("Load", "Load MWh", new_data $ variable) 괜찮 았는데 :) – ppi0trek