2017-12-11 22 views
0

を示されていません。ここで定義されmkplot()機能使用してgeom_textは、私はこのようなプロットを作成しました

enter image description here

mkplot <- function(m, title, subtitle = "", swap.axis = FALSE) { 
    if (swap.axis) { 
     ggplot(data = m, aes(x = Var1, y = Var2, fill = value, label = vtext)) + 
     xlab("") + ylab("") + 
     geom_tile() + 
     geom_text(aes(color = value > 0.6*max(m$value))) + 
     scale_color_manual(guide = FALSE, values = c("black", "white")) + #geom_text() + 
     scale_fill_gradient(low="beige", high="brown4") + 
     # Sample code for subtitles: ggtitle(bquote(atop("Age distribution", atop(italic(.(subtitle)), "")))) 
     ggtitle(bquote(atop(.(title), atop(italic(.(subtitle)), "")))) + 
     theme(axis.text.y = element_text(size = 12), axis.text.x = element_text(size = 12), 
       axis.title = element_text(size = 16, face = "bold"), 
       plot.title = element_text(size = 20), 
       panel.background = element_rect(fill = "white"), 
       legend.key.size = unit(0.02, "npc"), 
       legend.text = element_text(size = 14), 
       legend.title = element_text(size = 16)) 
    } else { 
     ggplot(data = m, aes(x = Var2, y = Var1, fill = value, label = vtext)) + 
     xlab("") + ylab("") + 
     geom_tile() + 
     geom_text(aes(color = value > 0.6*max(m$value))) + 
     scale_color_manual(guide = FALSE, values = c("black", "white")) + #geom_text() + 
     scale_fill_gradient(low="beige", high="brown4") + 
     # Sample code for subtitles: ggtitle(bquote(atop("Age distribution", atop(italic(.(subtitle)), "")))) 
     ggtitle(bquote(atop(.(title), atop(italic(.(subtitle)), "")))) + 
     theme(axis.text.y = element_text(size = 12), axis.text.x = element_text(size = 12), 
       axis.title = element_text(size = 16, face = "bold"), 
       plot.title = element_text(size = 20), 
       panel.background = element_rect(fill = "white"), 
       legend.key.size = unit(0.02, "npc"), 
       legend.text = element_text(size = 14), 
       legend.title = element_text(size = 16)) 
    } 

} 

にこの関数は、vtext行列を受け入れます列には、Var1Var2の列で定義された座標に表示されるラベルが含まれ、プロットオブジェクトが返されます。私は、次のデータセットで関数を呼び出すときただし、テキストは表示されません。

enter image description here

m <- structure(list(Var1 = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L), .Label = c("FE", "AG", 
"NO", "SPH", "SEP", "H/I", "CMP"), class = "factor"), Var2 = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L 
), .Label = c("FE", "AG", "NO", "SPH", "SEP", "H/I", "CMP"), class = "factor"), 
    value = c(NA, 4.02149347178743, 1.91571478755385, 1.97927899702546, 
    2.02603899244707, 2.16281145091151, 1.59833957894136, 3.17739392983327, 
    NA, 1.64295952963793, 1.61938283892958, 2.01876401368226, 
    2.27583054552681, 1.92162040673626, 1.50343303384468, 1.5912590928444, 
    NA, 1.47906635805171, 1.26558011163096, 1.44998903804508, 
    1.37770174407686, 1.93309549042729, 1.74420096205238, 1.90134281743486, 
    NA, 1.27695246179966, 1.54810486167689, 1.56842318564524, 
    2.81544501207105, 1.94591127739177, 1.82503474449433, 1.66343561213693, 
    NA, 4.37746171596392, 2.85325226018081, 2.08623825385336, 
    2.0233480547976, 1.75090243733866, 1.89848629646063, 2.63683490455119, 
    NA, 2.68804806948658, 1.75131933951687, 1.47327632282202, 
    1.84345393451446, 1.74482341831917, 2.57428820233227, 3.01688030649311, 
    NA), vtext = c(NA, "4.0", "1.9", "2.0", "2.0", "2.2", "1.6", 
    "3.2", NA, "1.6", "1.6", "2.0", "2.3", "1.9", "1.5", "1.6", 
    NA, "1.5", "1.3", "1.4", "1.4", "1.9", "1.7", "1.9", NA, 
    "1.3", "1.5", "1.6", "2.8", "1.9", "1.8", "1.7", NA, "4.4", 
    "2.9", "2.1", "2.0", "1.8", "1.9", "2.6", NA, "2.7", "1.8", 
    "1.5", "1.8", "1.7", "2.6", "3.0", NA)), .Names = c("Var1", 
"Var2", "value", "vtext"), row.names = c(NA, -49L), class = "data.frame") 

p <- mkplot(ma, "All breeds", "HIGH/LOW ratio") 

plot(p) 

私はここに欠けている何か?何が間違っているのか分かりません。

答えて

1

ファンクションにna.rm = TRUE引数を追加する必要があります。テキスト行は次のようになります。

geom_text(aes(color = value > 0.6 * max(m$value, na.rm = TRUE))) 

PS:

あなたはこのようなあなたの機能を簡素化することができます:

mkplot <- function(m, title = NULL, subtitle = NULL, swap.axis = FALSE) { 
    library(ggplot2) 
    p <- ggplot(m, aes(Var1, Var2, fill = value, label = vtext)) + 
     labs(x = NULL, y = NULL) + 
     geom_tile() + 
     geom_text(aes(color = value > 0.6 * max(m$value, na.rm = TRUE))) + 
     scale_color_manual(guide = FALSE, values = c("black", "white")) + 
     scale_fill_gradient(low="beige", high="brown4") + 
     ggtitle(bquote(atop(.(title), atop(italic(.(subtitle)), "")))) + 
     theme(axis.text = element_text(size = 12), 
       axis.title = element_text(size = 16, face = "bold"), 
       plot.title = element_text(size = 20), 
       panel.background = element_rect(fill = "white"), 
       legend.key.size = unit(0.02, "npc"), 
       legend.text = element_text(size = 14), 
       legend.title = element_text(size = 16)) 
    if (swap.axis) { 
     p <- p + coord_flip() 
    } 
    return(p) 
} 
+0

ありがとう! @PoGibas。それは動作します! – user2641103

1

pblmはエラーを返しますcolour定義、から来ています:

0.6*max(m$value) 
[1] NA 

お試しください:ところで

ggplot(data = m, aes(x = Var1, y = Var2, fill = value, label = vtext)) + 
    xlab("") + ylab("") + 
    geom_tile() + 
    geom_text(aes(color = value > 0.6*max(m$value, na.rm = TRUE))) 

enter image description here

p <- mkplot(m, "All breeds", "HIGH/LOW ratio") 
plot(p) 

enter image description here

、ggplotsはあなたが任意の新しい関数呼び出しを追加することができますどのオブジェクトです。あなたのコードは、重複をたくさん持っているように、私はより多くのようになり、あなたの機能を示唆している:

mkplot <- function(m, title, subtitle = "", swap.axis = FALSE) { 
    if (swap.axis) { 
    call < - ggplot(data = m, aes(x = Var1, y = Var2, fill = value, label = vtext)) 
    } else { 
    call <- ggplot(data = m, aes(x = Var2, y = Var1, fill = value, label = vtext)) 
    } 
    call + 
    xlab("") + ylab("") + 
    geom_tile() + 
    geom_text(aes(color = value > 0.6*max(m$value, na.rm = TRUE))) + 
    scale_color_manual(guide = FALSE, values = c("black", "white")) + #geom_text() + 
    scale_fill_gradient(low="beige", high="brown4") + 
    # Sample code for subtitles: ggtitle(bquote(atop("Age distribution", atop(italic(.(subtitle)), "")))) 
    ggtitle(bquote(atop(.(title), atop(italic(.(subtitle)), "")))) + 
    theme(axis.text.y = element_text(size = 12), axis.text.x = element_text(size = 12), 
      axis.title = element_text(size = 16, face = "bold"), 
      plot.title = element_text(size = 20), 
      panel.background = element_rect(fill = "white"), 
      legend.key.size = unit(0.02, "npc"), 
      legend.text = element_text(size = 14), 
      legend.title = element_text(size = 16)) 
} 

また、この仕事をしていません{ggplot2}でcoord_flip機能があります:

mkplot <- function(m, title, subtitle = "", swap.axis = FALSE) { 
    ggcall <- ggplot(data = m, aes(x = Var1, y = Var2, fill = value, label = vtext)) + 
    xlab("") + ylab("") + 
    geom_tile() + 
    geom_text(aes(color = value > 0.6*max(m$value, na.rm = TRUE))) + 
    scale_color_manual(guide = FALSE, values = c("black", "white")) + #geom_text() + 
    scale_fill_gradient(low="beige", high="brown4") + 
    # Sample code for subtitles: ggtitle(bquote(atop("Age distribution", atop(italic(.(subtitle)), "")))) 
    ggtitle(bquote(atop(.(title), atop(italic(.(subtitle)), "")))) + 
    theme(axis.text.y = element_text(size = 12), axis.text.x = element_text(size = 12), 
      axis.title = element_text(size = 16, face = "bold"), 
      plot.title = element_text(size = 20), 
      panel.background = element_rect(fill = "white"), 
      legend.key.size = unit(0.02, "npc"), 
      legend.text = element_text(size = 14), 
      legend.title = element_text(size = 16)) 
    if (swap.axis) { 
    ggcall + coord_flip() 
    } else { 
    ggcall 
    } 
} 

p <- mkplot(m, "All breeds", "HIGH/LOW ratio") 
plot(p) 

enter image description here

pswap <- mkplot(m, "All breeds", "HIGH/LOW ratio", swap.axis = TRUE) 
plot(pswap) 

enter image description here

+0

ありがとう! @コリンフェイズそれは動作します!私はそれが最初に来たので、私は他の答えを受け入れましたが、ソリューションの結果とプロットを投稿していただきありがとうございます。 – user2641103

関連する問題