2017-12-11 10 views
0

은이 같은 플롯을 만들었다 도시되지geom_text는

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

인수를 max 함수에 추가해야합니다. 텍스트 라인은 다음과 같아야합니다

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 

다음으로 시도해보십시오. 그런데

enter image description here

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

enter image description here

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))) 
는 ggplots는 새로운 함수 호출을 추가 할 수있는 객체입니다. 코드가 중복을 많이 가지고, 나는 더 등이 될 수 있도록 기능을 제안 :

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

:

또한
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 기능이있다

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

enter image description here

+0

고마워요! @ 콜린 그게 효과가! 처음에는 다른 답변을 받아 들였지만 솔루션 결과로 줄거리를 게시 해 주셔서 감사합니다. – user2641103