3

私は ggplot2 を使用する初心者で、ヒートマップの上に散布図をプロットしようとしています。両方のプロットには、同じ個別の x 軸があります。

これは私が試しているコードです:

library(ggplot2)
library(grid)
library(reshape2)
#data for the scatterplot

df = data.frame(id1 = letters[1:10], C = abs(rnorm(10)))

#scatter plot 
p1 <- ggplot(df, aes(x= id1, y = C)) + 
  geom_point(pch = 19) + theme_bw() + 
  scale_x_discrete(expand = c(0, 0), breaks = letters[1:10]) +
  theme(legend.position = "none") + theme(axis.title.y = element_blank()) + theme(axis.title.x = element_blank())

#data for the heatmap
X = data.frame(matrix(rnorm(100), nrow = 10))
names(X) = month.name[1:10]
X = melt(cbind(id1 = letters[1:10], X))

#heatmap 
p2 <- ggplot(X,
             aes(x = id1, y = variable, fill = value))
p2 <- p2 + geom_tile()
p2 <- p2 + scale_fill_gradientn(colours = c("blue", "white" , "red"))
p2 <- p2 + theme(legend.position = "none") + theme(axis.title.y = element_blank()) + theme(axis.title.x = element_blank())
p2 <- p2 + scale_x_discrete(expand = c(0, 0), breaks = letters[1:10])
p2 <- p2 + scale_y_discrete(expand = c(0, 0))

layt <- grid.layout(nrow=2,ncol=1,heights=c(2/8,6/8),default.units=c('null','null'))

vplayout <- function(x,y) {viewport(layout.pos.row = x, layout.pos.col = y)}

grid.newpage()
pushViewport(viewport(layout=layt))
print(p1,vp=vplayout(1,1))
print(p2,vp = vplayout(2,1))

問題は、軸が上下に配置されていないことです。

https://mail.google.com/mail/u/0/?ui=2&ik=81975edabc&view=att&th=13ece12a06a3cea2&attid=0.1&disp=emb&realattid=ii_13ece128398baede&zw&atsh=1

解決策はありますか?データをリシェイプしてファセットのようなものを作ることは可能ですか?

4

2 に答える 2

2

別のオプション:

grid.draw(gtable:::rbind.gtable(ggplotGrob(p1),
                                ggplotGrob(p2), size='last'))

(理想的には が必要ですsize=maxが、動作を妨げるバグがあります)。

于 2013-05-22T23:48:32.993 に答える
1

ここにはいくつかのトリックがあります。1 つ目は、同じ個別の軸がある場合でも、目盛りの扱いが異なることです。するとexpand = c(0,0)、散布図では目盛りがy軸に揃えられ、ヒートマップではカテゴリの中心に配置されます。それを回避する私の方法は、散布図の値を手動で割り当ててexpand、カテゴリ値の 1/2 のギャップがあるようにすることです。10 個のカテゴリ値があるため、この場合は0.05( (1/10)/2) です。ポイントが各カテゴリの中心に配置されます。

問題のもう一方の側面は、yラベルのサイズが異なるため、残りの配置が破棄されることです。解決策は、パッケージを使用して、この質問から得られます。ggplot_gtablegrid.arrangegridExtra

library(gridExtra)
#data for the scatterplot

df = data.frame(id1 = letters[1:10], C = abs(rnorm(10)))

#scatter plot 
p1 <- ggplot(df, aes(x= id1, y = C)) + 
  geom_point(pch = 19) + theme_bw() + 
# Change the expand values
  scale_x_discrete(expand = c(0.05, 0.05), breaks = letters[1:10]) +
  #scale_y_discrete(breaks = NULL) +

  theme(legend.position = "none") + theme(axis.title.y = element_blank()) + theme(axis.title.x = element_blank())
p1
#data for the heatmap
X = data.frame(matrix(rnorm(100), nrow = 10))
names(X) = month.name[1:10]
X = melt(cbind(id1 = letters[1:10], X))

#heatmap 
p2 <- ggplot(X,
             aes(x = id1, y = variable, fill = value))
p2 <- p2 + geom_tile()
p2 <- p2 + scale_fill_gradientn(colours = c("blue", "white" , "red"))
p2 <- p2 + theme(legend.position = "none") + theme(axis.title.y = element_blank()) + theme(axis.title.x = element_blank())
p2 <- p2 + scale_x_discrete(expand = c(0, 0), breaks = letters[1:10])
p2 <- p2 + scale_y_discrete(expand = c(0, 0))

#Here's the gtable magic
gp1<- ggplot_gtable(ggplot_build(p1))
gp2<- ggplot_gtable(ggplot_build(p2))
#This identifies the maximum width
maxWidth = unit.pmax(gp1$widths[2:3], gp2$widths[2:3])
#Set each to the maximum width
gp1$widths[2:3] <- maxWidth
gp2$widths[2:3] <- maxWidth
#Put them together
grid.arrange(gp1, gp2)

yEDIT -軸の整列のより洗練された方法については、@baptiste の回答を参照してください。

ここに画像の説明を入力

于 2013-05-22T23:24:51.100 に答える