简体   繁体   English

ggplot2中的geom_text未显示

[英]geom_text in ggplot2 are not shown

I've created a plot like this: 我创建了一个这样的情节:

在此处输入图片说明

by using the mkplot() function defined here: 通过使用此处定义的mkplot()函数:

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))  
    }

}

This function accepts a matrix where the vtext column contains the labels to show at the coordinates defined by the columns Var1 and Var2 and returns a plot object. 此函数接受一个矩阵,其中vtext列包含要在列Var1Var2定义的坐标上显示的标签,并返回一个图对象。 However, when I call the function with the following dataset, the text is not shown: 但是,当我使用以下数据集调用该函数时,未显示文本:

在此处输入图片说明

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)

What I'm missing here? 我在这里想念的是什么? I cannot figure out what is wrong. 我不知道怎么了。

You have to add na.rm = TRUE argument in max function. 您必须在max函数中添加na.rm = TRUE参数。 Text line should look like this: 文字行应如下所示:

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

PS.: PS:

You can simplify your function like this: 您可以像这样简化您的功能:

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)
}

The pblm comes from you colour definition, which return an error : pblm来自您的colour定义,它返回一个错误:

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

Try with : 尝试:

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)))

在此处输入图片说明

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

在此处输入图片说明

By the way, ggplots are objects on which you can add any new function call. 顺便说一句,ggplots是可以在其上添加任何新函数调用的对象。 As your code has a lot of duplication, I would suggest your function to be more like : 由于您的代码有很多重复项,因此我建议您使用以下函数:

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)) 
}

Also, there's a coord_flip function in {ggplot2} that does this job: 另外,{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)

在此处输入图片说明

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM