2016-11-30 7 views
0

gridExtraパッケージを使用して、3つのプロットを重ねて表示しようとしています。私はgrid.arrangehereから使用する最初の例を試しましたが、これはまったく問題ありません。gridExtra(ggplot)を使用した面白い動作

私自身のプロットを使用しようとすると、各プロットの軸は取得されますが、データはなくなり、すべての書式が取り除かれます。最小作業例:

library(ggplot2) 
library(gridExtra)  

popu_H0 <- seq(10, 30, length=100) 
popu_H0_norm <- dnorm(popu_H0, mean = 20, sd = 4) 

popu_H0_df <- as.data.frame(cbind(popu_H0, popu_H0_norm)) 
plot_H0 <- ggplot(popu_H0_df, aes(x=popu_H0, y=popu_H0_norm)) 
plot_H0 + 
    geom_line() + 
    theme(
    text = element_text(size=20), 
    axis.title.x = element_text(vjust=0.1), 
    axis.text.x = element_text(size = rel(1.8)), 
    legend.position = "none", 
    axis.title.y = element_blank(), 
    axis.text.y = element_blank(), 
    axis.ticks.y = element_blank(), 
    axis.line.y = element_blank() 
) + 
    xlab("New label") + 
    annotate("text", x = 20, y = 0.05, label = "Some annotation", size = 10) 

grid.arrange(plot_H0, plot_H0, plot_H0, ncol = 1, nrow = 3) 

ggplotは、期待される出力を生成しますが、grid.arrangethisを生成します。

答えて

1

プロットオブジェクトの置き換えを忘れました。

library(ggplot2) 
library(gridExtra) 
popu_H0 <- seq(10, 30, length=100) 
popu_H0_norm <- dnorm(popu_H0, mean = 20, sd = 4) 

popu_H0_df <- as.data.frame(cbind(popu_H0, popu_H0_norm)) 
plot_H0 <- ggplot(popu_H0_df, aes(x=popu_H0, y=popu_H0_norm)) 
plot_H0 <- plot_H0 + # Here you need `<-` to update the plot 
    geom_line() + 
    theme(
    text = element_text(size=20), 
    axis.title.x = element_text(vjust=0.1), 
    axis.text.x = element_text(size = rel(1.8)), 
    legend.position = "none", 
    axis.title.y = element_blank(), 
    axis.text.y = element_blank(), 
    axis.ticks.y = element_blank(), 
    axis.line.y = element_blank() 
) + 
    xlab("New label") + 
    annotate("text", x = 20, y = 0.05, label = "Some annotation", size = 10) 

grid.arrange(plot_H0, plot_H0, plot_H0, ncol = 1, nrow = 3) 
+0

返信いただきありがとうございます。しかし、私は何かを逃していますか?あなたが(正確に)ライブラリ呼び出しを上に追加したことを除いて、投稿したコードは私と同じです。おそらく手掛かりは "あなたがプロットオブジェクトを置き忘れた"ということですが、私はこれが何を意味するのか分かりません!私が同じプロットを3回使用したという事実を言及しているならば、その振る舞いが3つの異なるプロットオブジェクトと全く同じであることを確認できます。私は例を単純で再現性のあるものにするためにこの方法で投稿しました。 – suknat

+0

私は第7行に 'plot_H0 < - 'を持っています。そこにコメントを追加します。 –

+0

華麗でありがとう!申し訳ありませんが、私はそれを見つけませんでした! – suknat