【问题标题】:R Build a customized ggplot within another function with multiple y axisR在具有多个y轴的另一个函数中构建自定义ggplot
【发布时间】:2016-02-27 00:52:50
【问题描述】:

我想为给定的 data.frame 构建一个 ggplot 图,其中包含一个 x 轴和多个 y.curves。另外,我想在自定义函数中执行此操作,因此我可以随时调用此函数,我想用各种数据框绘制一些东西。

我正在尝试开发的脚本是:

graph.date <- function(data, y.axis1, y.axis2, y.axis3, y.axis4, y.axis5, y.axis6, y.axis7, x.axis, y.lab, title, ...){
                ggplot(data, aes_string(x = x.axis)) +
                ylab(label = y.lab) + xlab(label = "Date") +
                ggtitle(label = title) +
                scale_x_date(breaks = "1 month", labels = date_format("%d-%b-%Y")) +
                geom_line(aes(y = y.axis1, colour = y.axis1), size = 1) +
                geom_line(aes(y = y.axis2, colour = y.axis2), size = 1) +
                geom_line(aes(y = y.axis3, colour = y.axis3), size = 1) +
                geom_line(aes(y = y.axis4, colour = y.axis4), size = 1) +
                geom_line(aes(y = y.axis5, colour = y.axis5), size = 1) +
                geom_line(aes(y = y.axis6, colour = y.axis6), size = 1) +
                geom_line(aes(y = y.axis7, colour = y.axis7), size = 1) +
                scale_fill_discrete() + scale_color_manual(values = c(brewer.pal(9, "Set1"), brewer.pal(9, "Set1"))) +
                labs(colour = "") + theme(plot.title = element_text(size = rel(1.76))) +
                guides(colour = guide_legend(override.aes = list(size=3))) +
                theme(text = element_text(size=20), axis.title=element_text(size=34,face="bold"), axis.text.x = element_text(face="bold",
                      color="black", size=24, angle=25), axis.text.y = element_text(face="bold", color="black", size=24, angle=0))
                } 

然后我调用函数:

graph.date(data = BelgiumMerged, y.axis1 = "Gen1", y.axis2 = "Gen2", y.axis3 = "Gen3",
           x.axis = "Date", y.lab = "Capacity", title = "title")

我得到的错误是:

eval 中的错误(expr、envir、enclos):找不到对象“y.axis1”

【问题讨论】:

    标签: r function dataframe ggplot2


    【解决方案1】:

    您得到的错误是 df 没有名为 y.axis1 的列。引用名称存储在变量 y.axis1 中的列的最简单方法是使用 aes_string() 而不是 aes()。也不要在对 aes() 的调用中设置颜色

    所以改变一切

       geom_line(aes(y = y.axis1, colour = y.axis1), size = 1)
    

       geom_line(aes_string(y = y.axis1), size = 1,color="red") # Or whatever color you want 
    

    但是,解决该问题的更好方法是将数据框重新整形为长格式,以便所有 x 坐标在一列中,所有 y 坐标在一列中,并将这些分组在第三列中。然后你的函数可以定义为

    graph.date <- function(df,y.axes,x.axis){
        index <- which(names(df) %in% y.axes)
        plotDF <- gather(df,y.type,y.data,index)
    
        ggplot(plotDF,aes_string(x.axis)) + 
        geom_line(mapping=aes(y=y.data,color=y.type))
    }
    

    在这里您将传递一个 y 轴向量,而不是每个 y 轴都有一个参数

    【讨论】:

    • 感谢您的快速回复。这部分解决了问题,但我如何自动为情节分配图例?我尝试了 3 个变量:“geom_line(aes_string(y = y.axis1), size = 1, color = aes_string(y = y.axis1)) + geom_line(aes_string(y = y.axis2), size = 1, color = aes_string(y = y.axis2)) + geom_line(aes_string(y = y.axis3), size = 1, color = aes_string(y = y.axis3)) +" 但它们都得到默认的黑色跨度>
    【解决方案2】:

    非常感谢 Nist - 你是摇滚明星

    我的最终脚本如下所示:

    graph.date <- function(data,y.axes,x.axis, y.lab, x.lab, title){
      index <- which(names(data) %in% y.axes)
      plotDF <- gather(data,y.type,y.data,index)
    
      ggplot(plotDF,aes_string(x.axis)) + ggtitle(label = title) + ylab("Capacity [MW]") + xlab("Date") +
        geom_line(mapping=aes(y=y.data,color=y.type))+
        scale_fill_discrete() + scale_x_date(breaks = "1 month", labels = date_format("%d-%b-%Y")) +
        scale_color_manual(values = c(brewer.pal(9, "Set1"), brewer.pal(9, "Set1"))) + 
        labs(colour = "LegendTitle") + theme(plot.title = element_text(size = rel(1.76))) + 
        guides(colour = guide_legend(override.aes = list(size=3))) +
        theme(text = element_text(size=20), axis.title=element_text(size=34,face="bold"),
              axis.text.x = element_text(face="bold", color="black", size=24, angle=25),
              axis.text.y = element_text(face="bold", color="black", size=24, angle=0))
    }
    
    #Calling the function
    graph.date(df, y.axes = c("Gen1", "Gen2", "Gen3"), x.axis = "Date", title = "title")
    

    【讨论】:

      猜你喜欢
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 2019-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      相关资源
      最近更新 更多