2017-12-06 13 views
1

私はggplot2を使ってパーセントのヒートマップを作成しようとしています。私は次のコードを試しました。エースのwidthオプションが問題を解決すると信じていました。ggplotから余分なスペースを取り除く方法

p_prev_tg <- ggplot(tg_melt, aes(x = variable , y = OTU, fill = value, 
        width=.3)) + geom_tile() + 
        scale_fill_gradientn(colours = hm.palette2(10)) + 
        xlab(NULL) + ylab(NULL) + 
        theme(axis.text=element_text(size=7)) 
p_prev_tg 

残念ながら、これは図のように空き領域が多いプロットを返します。私が望むプロットは、2つのバーが並んでいます。これをggplotでどうやって行うことができますか?

heatmap

おかげ

+0

は '幅= .3' –

+0

を削除してみてください、私は当初、それを試してみましたが、その後タイルとプロットの両方があまりにも広いです。 – adamsorbie

+0

'+ coord_fixed()'またはおそらく '+ coord_fixed(expand = FALSE)'で、幅を設定しません。 – Axeman

答えて

1

何この解決策は?

set.seed(1234) 
tg_melt <- data.frame(variable=rep(c("Prevalence_T","Prevalence_NT"), each=10), 
         OTU=rep(paste0("OTU_",1:10),2), 
         value=rnorm(20)) 

library(RColorBrewer) 
library(ggplot2) 
hm.palette2 <- colorRampPalette(rev(brewer.pal(11, 'Spectral'))) 

p_prev_tg <- ggplot(tg_melt, aes(x = as.numeric(variable), y = OTU, fill = value)) + 
        geom_tile() + 
        scale_fill_gradientn(colours = hm.palette2(10)) + 
        xlab(NULL) + ylab(NULL) + 
        theme(axis.text=element_text(size=7)) + 
        scale_x_continuous(breaks=c(1,2), 
         limits=c(0,3), 
         labels=levels(tg_melt$variable))+ 
        theme_bw() 

p_prev_tg 

enter image description here

関連する問題