绘图函数给出的数据必须是矢量类型,为'NULL'

时间:2016-09-09 14:07:04

标签: r plot ggplot2 null

所以我使用以下函数绘制我必须绘制的大部分数据。我创建了它,这要归功于我在网上找到的不同代码块。到目前为止,我从未遇到任何问题。 这是首先绘图功能。

library(ggplot2)
library(reshape2)

#' Plot a given mean with error bars
#' @param resultTable The table with all the result to plot
#' @param techniques The name of the techniques in the form of a list/vector
#' @param nbTechs The number of given techniques
#' @param ymin The minimum value for y
#' @param ymax The maximum value for y
#' @param xAxisLabel The label for the x (vertical) axis
#' @param yAxisLable The label for the y (horizontal) axis
#' @return 
#' 
barChartTime <- function(resultTable, techniques, nbTechs = -1, ymin, ymax, xAxisLabel = "I am the X axis", yAxisLabel = "I am the Y Label"){
  #tr <- t(resultTable)
  if(nbTechs <= 0){
    stop('Please give a positive number of Techniques, nbTechs');
  }

  tr <- as.data.frame(resultTable)
  nbTechs <- nbTechs - 1 ; # seq will generate nb+1

  #now need to calculate one number for the width of the interval
  tr$CI2 <- tr$upperBound_CI - tr$mean_time
  tr$CI1 <- tr$mean_time - tr$lowerBound_CI

  #add a technique column
  tr$technique <- factor(seq.int(0, nbTechs, 1));

  breaks <- c(as.character(tr$technique));
  print(tr)
  g <- ggplot(tr, aes(x=technique, y=mean_time)) + 
    geom_bar(stat="identity",fill = I("#CCCCCC")) +
    geom_errorbar(aes(ymin=mean_time-CI1, ymax=mean_time+CI2),
                  width=0,                    # Width of the error bars
                  size = 1.1
    ) +
    #labs(title="Overall time per technique") +
    labs(x = xAxisLabel, y = yAxisLabel) + 
    scale_y_continuous(limits = c(ymin,ymax)) +
    scale_x_discrete(name="",breaks,techniques)+
    coord_flip() +
    theme(panel.background = element_rect(fill = 'white', colour = 'white'),axis.title=element_text(size = rel(1.2), colour = "black"),axis.text=element_text(size = rel(1.2), colour = "black"),panel.grid.major = element_line(colour = "#DDDDDD"),panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())+
    geom_point(size=4, colour="black")         # dots

  print(g)
}

现在,这是我正在使用的(数据的简化版本)数据(并重现错误):

EucliP,AngularP,EucliR,AngularR,EucliSp,AngularSp,EucliSl,AngularSl
31.6536,30.9863,64.394,92.7838,223.478,117.555,44.7374,25.4852
12.3592,40.7639,70.2508,176.55,10.3927,145.909,143.025,126.667
14.572,8.98445,113.599,150.551,47.1545,54.3019,10.7038,47.7004
41.7957,20.9542,55.1732,67.1647,52.364,41.3655,62.7036,75.65
135.868,83.7135,14.0262,69.7183,44.987,35.9599,19.5183,66.0365
33.5359,17.2129,6.95909,47.518,224.561,91.4999,67.1279,31.4079
25.7285,33.6705,17.4725,58.45,43.1709,113.847,28.9496,20.0574
48.4742,127.588,75.0804,89.1176,31.4494,27.9548,38.4563,126.248
31.9831,80.0161,19.9592,145.891,55.2789,142.738,94.5126,136.099
17.4044,52.3866,49.9976,150.891,104.936,77.2849,232.23,35.6963
153.359,151.897,41.8876,46.3893,79.5218,75.2011,68.9786,91.8972

这是我正在使用的代码:

data = read.table("*Path_to_file*.csv", header=T, sep=",")
data$EucliPLog = (data$EucliP) #Before here I used to use a log transform that I tried to remove for some testing
data$EucliRLog = (data$EucliR) #Same thing
data$EucliSpLog = (data$EucliSp) #Same thing
data$EucliSlLog = (data$EucliSl) #Same thing
a1 = t.test(data$EucliPLog)$conf.int[1]
a2 = t.test(data$EucliPLog)$conf.int[2]
b1 = t.test(data$EucliRLog)$conf.int[1]
b2 = t.test(data$EucliRLog)$conf.int[2]
c1 = t.test(data$EucliSpLog)$conf.int[1]
c2 = t.test(data$EucliSpLog)$conf.int[2]
d1 = t.test(data$EucliSlLog)$conf.int[1]
d2 = t.test(data$EucliSlLog)$conf.int[2]
analysisData = c()
analysisData$ratio = c("Sl","Sp","R","P")
analysisData$pointEstimate = c(exp(mean(data$EucliSlLog)),exp(mean(data$EucliSpLog)),exp(mean(data$EucliRLog)),exp(mean(data$EucliPLog)))
analysisData$ci.max = c(exp(d2), exp(c2),exp(b2), exp(a2))
analysisData$ci.min = c(exp(d1), exp(c1),exp(b1), exp(a1))

datatoprint <- data.frame(factor(analysisData$ratio),analysisData$pointEstimate, analysisData$ci.max, analysisData$ci.min)
colnames(datatoprint) <- c("technique", "mean_time", "lowerBound_CI", "upperBound_CI ")
barChartTime(datatoprint,analysisData$ratio ,nbTechs = 4, ymin = 0, ymax = 90, "", "Title")

所以,如果我使用我在最后一段代码的评论中提到的log(),一切正常,我会显示我的情节。但是,我尝试删除日志,我得到了着名的

Error in matrix(value, n, p) : 
  'data' must be of a vector type, was 'NULL'

我曾尝试在我的数据中查找空值,但没有,我不知道下一步该在哪里查看。很想得到一些帮助。 提前致谢

修改:以下是dputdatatoprint的结果:

structure(list(technique = structure(c(3L, 4L, 2L, 1L), .Label = c("P", 
"R", "Sl", "Sp"), class = "factor"), mean_time = c(1.04016257618464e+32, 
1.64430609815788e+36, 7.5457775364611e+20, 3.85267453902928e+21
), lowerBound_CI = c(6.64977706609883e+50, 5.00358136618364e+57, 
2.03872433045407e+30, 4.93863589006376e+35), `upperBound_CI ` = c(16270292584857.9, 
540361462434140, 279286207454.44, 30055062.6409769)), .Names = c("technique", 
"mean_time", "lowerBound_CI", "upperBound_CI "), row.names = c(NA, 
-4L), class = "data.frame")

dput上的analysisData

structure(list(ratio = c("Sl", "Sp", "R", "P"), pointEstimate = c(1.04016257618464e+32, 
1.64430609815788e+36, 7.5457775364611e+20, 3.85267453902928e+21
), ci.max = c(6.64977706609883e+50, 5.00358136618364e+57, 2.03872433045407e+30, 
4.93863589006376e+35), ci.min = c(16270292584857.9, 540361462434140, 
279286207454.44, 30055062.6409769)), .Names = c("ratio", "pointEstimate", 
"ci.max", "ci.min"))

1 个答案:

答案 0 :(得分:2)

没有日志我没有显示任何内容,因为值大于10 ^ 40 ++而日志则低于上限(90)。
我不会得到你得到的错误。