r语言 - 如何解决提供给连续刻度和 plot$scales$clone() 的离散值:尝试应用非函数错误



我目前正在尝试自动绘制一些散点图。当我尝试创建包含所有散点图的 pdf 文件时,我遇到了一个错误,我得到Error: Discrete value supplied to continuous scaleError in Summary.factor(c(3L, 4L, 1L, 2L), na.rm = FALSE) : ‘max’ not meaningful for factorsError in plot$scales$clone() : attempt to apply non-function。我不确定如何解决此问题。下面您将找到我的代码以及其中一个.csv文件的示例。

id,tag_name,covpercent,dayDifference, dayDifferenceHours
4404155,0.8,0.809478672985782,38 days, 22:13:21
8814971,0.9,0.83478672985782,416 days, 9:31:02
4410508,1.4,0.84838672985782,123 days, 17:49:45
7399931,1.5,0.84933272985782,301 days, 22:18:32
library(tidyverse)
library(ggplot2)

csv_list = list.files(pattern="*.csv")
print(csv_list)
plot_list <- list()
for(i in 1:length(csv_list)){
  data_frame <-read.csv(csv_list[i], header =  TRUE)
  gg <- ggplot(data = data_frame , mapping = aes(x = covpercent, y = dayDifference)) +
    geom_point() +
    # Not sure if to use ylim?
    # for some reason getting the max days like this doesn't work #max(data_frame$dayDifference)
    # So I set it to 365 days
    scale_y_continuous(limits=c(0,max(data_frame$dayDifference)))
    labs(x = "Code coverage",
         y = "Number of days between releases",
         title = basename(csv_list[i]) # just file name, not whole path
    )
  ggsave(filename = sub('\.csv$', '.png', csv_list[i]), device = "png", plot = gg) # change file extension to indicate output format

  # to store plot to a list do
  #plot_list[[length(plot_list)+1]] <- gg
}
#
# Tried to produce all graphs in one pdf but I get an error
# Error in plot$scales$clone() : attempt to apply non-function
# store all plots in 1 pdf-file:
class(plot_list) <- c('arrangelist', class(ggplot()))
ggsave(file.path(dirname(csv_list[1]), 'all_plots.pdf'), plot_list, width = 25.6, height=16, units='cm', scale=1.5, dpi=1000)

现在dayDifference类是"difftime"。您必须将其转换为数字。这应该生成您的个人绘图:

data_frame <-read.csv(csv_list[i], header =  TRUE)
data_frame$dayDifference <- as.numeric(gsub("[^[:digit:]]","",data_frame$dayDifference))
gg <- ggplot(data = data_frame , mapping = aes(x = covpercent, y = dayDifference)) +
  geom_point() +
  scale_y_continuous(limits=c(0,max(data_frame$dayDifference))) +
  labs(x = "Code coverage",
       y = "Number of days between releases",
       title = basename(csv_list[i])
  )
gg

最新更新