简体   繁体   中英

User input name to ggplot

I am writing a function to plot heat map for users. In the following example, it plots the change of grade over time for different gender.

However, this is a special case. "Gender" may have other name like "Class". I will let user input their specific name and then make ggplot have the right label for each axis.

How do I modify my function " heatmap() " based on what I need?

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

The easiest solution is redefining the function using aes_string. When the function is called, you need to pass it the name of the column you want to use as a 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")

Alternatively if you want to retain the quote free syntax, there is a slightly more complex solution:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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