Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 未显示ggplot2中的几何图形文字_R_Plot_Ggplot2 - Fatal编程技术网

R 未显示ggplot2中的几何图形文字

R 未显示ggplot2中的几何图形文字,r,plot,ggplot2,R,Plot,Ggplot2,我创造了一个这样的情节: geom_text(aes(color = value > 0.6 * max(m$value, na.rm = TRUE))) mkplot <- function(m, title = NULL, subtitle = NULL, swap.axis = FALSE) { library(ggplot2) p <- ggplot(m, aes(Var1, Var2, fill = value, label = vtext)) +

我创造了一个这样的情节:

geom_text(aes(color = value > 0.6 * max(m$value, na.rm = TRUE)))
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)
}

通过使用此处定义的
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))  
    }

}

我错过了什么?我想不出是怎么回事

您必须在
max
函数中添加
na.rm=TRUE
参数。文本行应如下所示:

geom_text(aes(color = value > 0.6 * max(m$value, na.rm = TRUE)))
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)
}
PS.:

您可以这样简化您的函数:

geom_text(aes(color = value > 0.6 * max(m$value, na.rm = TRUE)))
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)
}

mkplot您必须在
max
函数中添加
na.rm=TRUE
参数。文本行应如下所示:

geom_text(aes(color = value > 0.6 * max(m$value, na.rm = TRUE)))
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)
}
PS.:

您可以这样简化您的函数:

geom_text(aes(color = value > 0.6 * max(m$value, na.rm = TRUE)))
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)
}

mkplotpblm来自您的
color
definition,返回错误:

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


pswappblm来自您的
color
definition,它返回一个错误:

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


pswap谢谢@科林费,行得通!我已经接受了另一个答案,因为它是第一个答案,但是谢谢你发布了一个包含解决方案结果的图。谢谢@科林费,行得通!我接受了另一个答案,因为它是第一个答案,但感谢您发布了一个包含解决方案结果的图。