2016-12-08 5 views
2

3つのggplotヒストグラムを結合したいと思います。これを行うには、gridExtraパッケージを使用しています。すべてのプロットが1行にあるので、右から数えて2つのプロットからyのタイトルとスケールを削除したい。ggplotからyラベルを削除する

私はいつもと同じコードを書いたが、うまくいかなかった。あなたは何が問題かもしれないか知っていますか?私のコード:

plot1 <- ggplot(testing, aes(x=residualtotal))+ 
    geom_histogram(aes(y = ..density..), binwidth = 100) + 
    geom_density(aes(y = ..density..*(2)))+ 
    xlab("Residuals Model 1 [MW]")+ 
    theme(panel.background=element_rect(fill = "white"))+ 
    theme_minimal() 
plot2 <- ggplot(testing, aes(x=residualtotal1))+ 
    geom_histogram(aes(y = ..density..), binwidth = 100) + 
    geom_density(aes(y = ..density..*(2)))+ 
    xlab("Residuals Model 2 [MW]")+ 
    theme(axis.text.y = element_blank(), axis.title.y = element_blank(), axis.ticks.y = element_blank(), panel.background=element_rect(fill = "white"))+ 
    theme_minimal() 
plot3 <- ggplot(testing, aes(x=residualtotal2))+ 
    geom_histogram(aes(y = ..density..), binwidth = 100) + 
    geom_density(aes(y = ..density..*(2)))+ 
    xlab("Residuals Model 3 [MW]")+ 
    theme(axis.text.y = element_blank(), axis.title.y = element_blank(), axis.ticks.y = element_blank(), panel.background=element_rect(fill = "white"))+ 
    theme_minimal() 
grid.arrange(plot1, plot2, plot3, ncol = 3, nrow=1) 

私のデータセットのサンプル。

Load residualtotal1 prognosis2 residualtotal2 residualtotal 
89 20524  -347.6772 20888.75  -364.7539 -287.82698 
99 13780  -133.8496 13889.52  -109.5207  -6.60009 
100 13598  -155.9950 13728.77  -130.7729  -27.18835 
103 13984  -348.4080 14310.12  -326.1226 -213.68816 
129 14237  -3141.5591 17375.82  -3138.8188 -3077.32236 
130 14883  -3142.0134 18026.02  -3143.0183 -3090.52193 
+0

それ以外の場合、 'theme_minimal'はそれをオーバーライドします。 – eipi10

+0

また、3つのプロットすべてで使用する任意のプロット要素をリストオブジェクトに入れることができるため、毎回繰り返す必要はありません。例: 'my_plot = list(geom_density(aes(y = ..density .. *(2)))、 theme_minimal()、 テーマ(axis.text.y = element_blank()、axis.title.y =あなたの 'ggplot'呼び出しに' + my_plot'をインクルードしてください。 – eipi10

+0

@ eipi10ありがとう、偉大な働きました:) – ppi0trek

答えて

1

私は、あなたが探していることは+ ylab(NULL)あるとtheme_minimal()後にtheme()を移動すると思います。また、幅の指定をgrid.arrangeに追加しました。これは、yのタイトルにスペースを与えるために、一番左の図形の幅を広くする必要があるからです。

あなたのコードは、代替アプローチ

plot1 <- ggplot(testing, aes(x=residualtotal))+ 
    geom_histogram(aes(y = ..density..), binwidth = 100) + 
    geom_density(aes(y = ..density..*(2)))+ 
    xlab("Residuals Model 1 [MW]")+ 
    theme_minimal() 
plot2 <- ggplot(testing, aes(x=residualtotal1))+ 
    geom_histogram(aes(y = ..density..), binwidth = 100) + 
    geom_density(aes(y = ..density..*(2)))+ 
    xlab("Residuals Model 2 [MW]")+ 
    ylab(NULL) + 
    theme_minimal() + 
    theme(axis.text.y = element_blank()) 
plot3 <- ggplot(testing, aes(x=residualtotal2))+ 
    geom_histogram(aes(y = ..density..), binwidth = 100) + 
    geom_density(aes(y = ..density..*(2)))+ 
    xlab("Residuals Model 3 [MW]")+ 
    ylab(NULL) + 
    theme_minimal() + 
    theme(axis.text.y = element_blank()) 
grid.arrange(plot1, plot2, plot3, ncol = 3, nrow=1, widths = c(1.35, 1, 1)) 

enter image description here

2

次のようになります。 `theme`文が*後*` theme_minimalを() `入れ

library(tidyverse) 

res_trans <- c(`residualtotal`="Residuals Model 1 [MW]", 
       `residualtotal1`="Residuals Model 2 [MW]", 
       `residualtotal2`="Residuals Model 3 [MW]") 

select(testing, starts_with("resid")) %>% 
    gather(which_resid, value) %>% 
    mutate(label=res_trans[which_resid]) %>% 
    ggplot(aes(x=value, group=label)) + 
    geom_histogram(aes(y = ..density..), binwidth = 100) + 
    geom_density(aes(y = ..density..*(2))) + 
    facet_wrap(~label, ncol=3) + 
    labs(x=NULL, y=NULL) + 
    theme_minimal() + 
    theme(panel.background=element_rect(fill = "white")) 

enter image description here

関連する問題