Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/84.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
ggave()错误:未知输入R_R_Ggplot2 - Fatal编程技术网

ggave()错误:未知输入R

ggave()错误:未知输入R,r,ggplot2,R,Ggplot2,我在使用ggplot2库中的ggsave()时遇到问题。我编写了一个将参数传递给的函数,该函数应该生成结果,然后使用ggsave()保存结果 下面是一些重现错误的示例数据和代码: example.df.1 <- data.frame(matrix(1:100, nrow = 20, ncol = 5)) colnames(example.df.1) <- c("var1", "var2", "var3", "var4", "var5") rownames(example.df.1)

我在使用ggplot2库中的ggsave()时遇到问题。我编写了一个将参数传递给的函数,该函数应该生成结果,然后使用ggsave()保存结果

下面是一些重现错误的示例数据和代码:

example.df.1 <- data.frame(matrix(1:100, nrow = 20, ncol = 5))
colnames(example.df.1) <- c("var1", "var2", "var3", "var4", "var5")
rownames(example.df.1) <- c("A", "B", "C", "D", "E", "F", "G", "H", 
                            "I", "J", "K", "L", "M", "N", "O", "P",
                            "Q", "R", "S", "T")

example.df.2 <- data.frame(matrix(ncol = 2, nrow = 24))
example.df.2[,1] <- c("A", "B", "C", "D", "E", "F", "G", "H", 
                            "I", "J", "K", "L", "M", "N", "O", "P",
                            "Q", "R", "S", "T", "U", "V", "W", "X")

example.df.2[,2] <- rnorm(24, 10, 2)                            

problematic_func <- function(data1, col, title, var, data2)  {
    # only include rows without missing values
    loc1 <- subset(data1, rowSums(is.na(data1)) == 0)

    loc1 <- cbind(loc1, rank(-as.data.frame(loc1[,1]), ties.method = "first"))

    # reduce data2 to only those rows that correspond to rows in data1
    loc2 <- data2[data2[,1] %in% rownames(loc1),]

    # order loc2
    loc2.ordered <- loc2[order(loc2[,1]),]

    # correlation between loc1 and loc2.ordered
    corr <- cor(loc1[,1], loc2.ordered[,2])

    # creating the plot
    i <- ggplot(loc1, aes_q(x = loc1[,1], y = loc2.ordered))
    i <- i + geom_point(colour = col, size = 4)
    i <- i + ggtitle(title)
    i <- i + xlab(var)
    i <- i + ylab("y-axis")
    i <- i + coord_cartesian(xlim = c(0, max(loc1[,1])),
        ylim = c(0, max(loc2.ordered[,2])*1.2))
    i <- i + annotate("text", x = max(loc1[,1])*.5, y = 1,
        label = paste("Correlation coef: ", as.character(corr)), size = 3)

    # saving the plot - this is where the error occurs according
    # to the debugger   
    ggsave(filename = paste("my_example_plot_", var, ".png", sep = ""),
        plot = i, device = png, width = 625, height = 625, limitsize = FALSE)
}

for (i in 1:ncol(example.df.1)) {
    sv <- as.data.frame(example.df.1[,i])
    rownames(sv) <- rownames(example.df.1)
    problematic_func(sv, "orange", colnames(example.df.1[i]),
        colnames(example.df.1[i]), data2 = example.df.2)
}

example.df.1我自己发现了错误。错误不在ggsave()函数中,而是在我最初创建绘图的ggplot()函数中。正确的代码必须是:

i <- ggplot(loc1, aes_q(x = loc1[,1], y = loc2.ordered[,2]))
i