【问题标题】:User input name to ggplotggplot 的用户输入名称
【发布时间】:2016-06-21 18:19:45
【问题描述】:

我正在编写一个函数来为用户绘制热图。在以下示例中,它绘制了不同性别的成绩随时间的变化。

但是,这是一种特殊情况。 “性别”可能有其他名称,如“类”。 我会让用户输入他们的具体名称,然后让 ggplot 为每个轴设置正确的标签。

如何根据需要修改函数“heatmap()”?

sampledata <- matrix(c(1:60,1:60,rep(0:1,each=60),sample(1:3,120,replace = T)),ncol=3)
colnames(sampledata) <- c("Time","Gender","Grade")
sampledata <- data.frame(sampledata)
heatmap=function(sampledata,Gender)
{
sampledata$Time <- factor(sampledata$Time)
sampledata$Grade <- factor(sampledata$Grade)
sampledata$Gender <- factor(sampledata$Gender)
color_palette <- colorRampPalette(c("#31a354","#2c7fb8", "#fcbfb8","#f03b20"))(length((levels(factor(sampledata$Grade)))))
ggplot(data = sampledata) + geom_tile( aes(x = Time, y = Gender, fill = Grade))+scale_x_discrete(breaks = c("10","20","30","40","50"))+scale_fill_manual(values =color_palette,labels=c("0-1","1-2","2-3","3-4","4-5","5-6",">6"))+  theme_bw()+scale_y_discrete(labels=c("Female","Male"))
}

【问题讨论】:

  • 我认为你需要aes_string。看看thisthis

标签: r function ggplot2


【解决方案1】:

最简单的解决方案是使用 aes_string 重新定义函数。 调用该函数时,您需要将列的名称传递给它 您想用作字符串。

heatmap=function(sampledata,y)
{
        sampledata$Time <- factor(sampledata$Time)
        sampledata$Grade <- factor(sampledata$Grade)
        sampledata$new_var <- factor(sampledata[,y])
        color_palette <- colorRampPalette(c("#31a354","#2c7fb8", "#fcbfb8","#f03b20"))(length((levels(factor(sampledata$Grade)))))
        ggplot(data = sampledata) + geom_tile( aes_string(x = "Time", y = "new_var", fill = "Grade"))+scale_x_discrete(breaks = c("10","20","30","40","50"))+scale_fill_manual(values =color_palette,labels=c("0-1","1-2","2-3","3-4","4-5","5-6",">6"))+  theme_bw()+scale_y_discrete(labels=c("Female","Male")) + ylab(y)
}


# Below an example of how you call the newly defined function
heatmap(sampledata, "Gender")

或者,如果您想保留无引号语法,还有一个稍微复杂的解决方案:

heatmap=function(sampledata,y)
{
        arguments <- as.list(match.call())
        axis_label <- deparse(substitute(y))
        y = eval(arguments$y, sampledata)

        sampledata$Time <- factor(sampledata$Time)
        sampledata$Grade <- factor(sampledata$Grade)
        sampledata$y <- factor(y)
        color_palette <- colorRampPalette(c("#31a354","#2c7fb8", "#fcbfb8","#f03b20"))(length((levels(factor(sampledata$Grade)))))
        ggplot(data = sampledata) + geom_tile( aes(x = Time, y = y, fill = Grade))+scale_x_discrete(breaks = c("10","20","30","40","50"))+scale_fill_manual(values =color_palette,labels=c("0-1","1-2","2-3","3-4","4-5","5-6",">6"))+  theme_bw()+scale_y_discrete(labels=c("Female","Male")) + ylab(axis_label)
}

# Below an example of how you call the newly defined function
heatmap(sampledata, Gender)

【讨论】:

  • 非常感谢!还有一个问题:在第二个函数中,我们仍然使用包含 Gender 的 'sampledata$Gender
  • 是的,我忘了更改那部分。我已经编辑了这两个代码以使它们更通用。现在一切正常。
  • 如果我希望 y 轴的标签是“Gender”而不是“new_var”和“y”怎么办。 (性别与用户输入一致,也可以是其他名字)
  • 我已经编辑了代码。现在 y 标签将与您传递给函数的变量具有相同的名称。在第一个函数中,您只需将变量名称传递给 ylab,因为它已经是一个字符串。在第二个开头有一些额外的代码来提取名称。我想要更多的布朗尼积分:P
  • 非常感谢!这真的很有帮助。我一直在寻找,但无法弄清楚。现在完美运行!
猜你喜欢
  • 1970-01-01
  • 2017-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
  • 1970-01-01
  • 2020-11-04
  • 1970-01-01
相关资源
最近更新 更多