2016-11-30 6 views
0

のように私の入力データが見えます:私は「PROGRESSのいくつかのテキスト(値を追加するヒートマップを生成Rのggplot2:ヒートマップの両側に伝説と値カウントにカスタムテキストを追加すること

COMPANY  DOMAIN REVIEW PROGRESS 
Company A Service Good  + 
Company A Response Good  + 
Company A Delay  Very Good  
Company A Cost  Poor  - 
Company B Service Poor  - 
Company B Delay  Average 
Company B Cost  Good  + 
Company C Service Very Poor + 
Company C Cost  Average 

"変数 - すなわちプラス記号またはマイナス記号)。ここでheatmap produced

は私のコードです:

require("ggplot2") 

graph <- read.table("input.tab", header=T, sep="\t") 

ggplot(data=graph, aes(x=COMPANY, y=DOMAIN, group=REVIEW, fill=REVIEW)) + 

geom_tile() + 

geom_text(aes(x=COMPANY, y=DOMAIN, label=PROGRESS)) + 

scale_x_discrete(expand = c(0, 0)) + 

scale_y_discrete(expand = c(0, 0)) + 

geom_vline(xintercept=seq(1.5, length(graph$COMPANY)+0.5)) + 
geom_hline(yintercept=seq(1.5, length(graph$DOMAIN)+0.5)) + 

theme(
    panel.grid.major = element_blank(), 
    panel.grid.minor = element_blank(), 
    axis.line = element_blank(), 
    axis.ticks = element_blank(), 
    panel.background = element_blank(), 
    plot.background = element_blank(), 
    axis.title=element_blank(), 
    axis.text.x = element_text(angle=45, size=12, hjust=1) 
    ) 

私は(図は手動で以下の変更を参照)を追加することに苦労していますしかし:すでに

(1)以下の「PROGRESS」の伝説カラーコードの一部として、記載されている:

+ Better 
- Worse 

(2)プロットの右側と凡例の間の各列上で利用可能なデータの数

プロット

enter image description here

の上に各列の利用可能なデータの(3)のカウント任意のアドバイスはありますか?

答えて

1

ここに私の提案する解決策があります。私が行ったことを理解するためのコードにコメントを追加しました。しかし、おそらくグリッドを生成するためのより良い方法があります。それが役に立てば幸い。

graph <- read_csv(
"COMPANY  ,DOMAIN ,REVIEW ,PROGRESS 
Company A ,Service ,Good  ,+ 
Company A ,Response ,Good  ,+ 
Company A ,Delay  ,Very Good , 
Company A ,Cost  ,Poor  ,- 
Company B ,Service ,Poor  ,- 
Company B ,Delay  ,Average , 
Company B ,Cost  ,Good  ,+ 
Company C ,Service ,Very Poor ,+ 
Company C ,Cost  ,Average ,") 


ggplot() + 
    # moved aesthetics and data to each geom, 
    # if you keep them in the ggplot call, 
    # you have to specify `inherit.aes = FALSE` in the rest of the geoms  
    geom_tile(data = graph, 
      aes(x = COMPANY, 
       y = DOMAIN, 
       fill = REVIEW)) + 
    # changed from `geom_text` to `geom_point` with custom shapes 
    geom_point(data = graph, 
      aes(x  = COMPANY, 
       y  = DOMAIN, 
       shape = factor(PROGRESS, labels = c("Worse", "Better"))), 
      size = 3) + 
    # custom shape scale 
    scale_shape_manual(name = "", values = c("-", "+")) + 
    # calculate marginal totals "on the fly" 
    # top total 
    geom_text(data = summarize(group_by(graph, COMPANY), 
           av_data = length(!is.na(PROGRESS))), 
      aes(x = COMPANY, 
       y = length(unique(graph$DOMAIN)) + 0.7, 
       label = av_data)) + 
    # right total 
    geom_text(data = summarize(group_by(graph, DOMAIN), 
           av_data = length(!is.na(PROGRESS))), 
      aes(x = length(unique(graph$COMPANY)) + 0.7, 
       y = DOMAIN, label = av_data)) + 
    # expand the plotting area to accomodate for the marginal totals 
    scale_x_discrete(expand = c(0, 0.8)) + 
    scale_y_discrete(expand = c(0, 0.8)) + 
    # changed to `geom_segment` to generate the grid, otherwise grid extends 
    # beyond the heatmap 
    # horizontal lines 
    geom_segment(aes(y = rep(0.5, 1 + length(unique(graph$COMPANY))), 
        yend = rep(length(unique(graph$DOMAIN)) + 0.5, 
           1 + length(unique(graph$COMPANY))), 
        x = seq(0.5, 1 + length(unique(graph$COMPANY))), 
        xend = seq(0.5, 1 + length(unique(graph$COMPANY))))) + 
    # vertical lines 
    geom_segment(aes(x = rep(0.5, 1 + length(unique(graph$DOMAIN))), 
        xend = rep(length(unique(graph$COMPANY)) + 0.5, 
           1 + length(unique(graph$DOMAIN))), 
        y = seq(0.5, 1 + length(unique(graph$DOMAIN))), 
        yend = seq(0.5, 1 + length(unique(graph$DOMAIN))))) + 

    # custom legend order 
    guides(fill = guide_legend(order = 1), 
     shape = guide_legend(order = 2)) + 
    # theme tweaks 
    theme(
    panel.grid.major = element_blank(), 
    panel.grid.minor = element_blank(), 
    axis.line  = element_blank(), 
    axis.ticks  = element_blank(), 
    panel.background = element_blank(), 
    plot.background = element_blank(), 
    axis.title  = element_blank(), 
    axis.text.x = element_text(angle = 45, 
           size = 12, 
           hjust = 1, 
           # move text up 20 pt 
           margin = margin(-20,0,0,0, "pt")), 
    # move text right 20 pt 
    axis.text.y = element_text(margin = margin(0,-20,0,0, "pt")) 
) 

final_plot

関連する問題