【问题标题】:Error in unique.default(x, nmax = nmax) : unique() applies only to vectors in ggplotunique.default(x, nmax = nmax) 中的错误:unique() 仅适用于 ggplot 中的向量
【发布时间】:2018-10-08 18:58:42
【问题描述】:

当我尝试运行此代码时,会出现标题中的错误。我是初学者,不明白如何解决它。我已包含所有代码以供参考。 ggplot 行之前的所有行似乎都有效,但最后一行会导致错误。

ClassStroopData_300 <- read_csv("ClassStroopData_300.csv")
Data300 <-ClassStroopData_300
mean(Data300$responseTime)
sd(Data300$responseTime)
Data300["WorkspaceVar1"] <- grepl("_",Data300$note)
WorkspaceVar1 that contains the info whether or not the column "note" contained a "_"
Data300["WorkspaceVar2"] <- grepl("text",Data300$content)
Data300["WorkspaceVar3"] <-grepl("red_red|blue_blue|green_green|orange_orange|purple_purple",Data300$content)
Data300["WorkspaceVar4"] <- grepl("4db78970c3816d3d|a8cc6a79046e188f|c21c48d74bc99a6|dd2942c485109f3e|5eb0fbeb9b0685e2",Data300$frameId)
Data300["incongruent"] <- Data300$WorkspaceVar1*2
Data300["instructions"] <- Data300$WorkspaceVar2*1
Data300["congruent"] <- Data300$WorkspaceVar3*3
Data300["practice"] <- Data300$WorkspaceVar4*10
Data300["condition sums"] <- Data300$incongruent + Data300$instructions + Data300$congruent + Data300$practice
Data300["condition"] <- factor(Data300$`condition sums`,levels = c("1","2","3","12"),labels = c("instructions","incongruent","congruent","practice"))
CongruentTrials <- subset(Data300, condition == "congruent")
mean(CongruentTrials$responseTime)
sd(CongruentTrials$responseTime)
IncongruentTrials <- subset(Data300, condition == "incongruent")
mean(IncongruentTrials$responseTime)
sd(IncongruentTrials$responseTime)
aggregate(Data300$responseTime,list(Data300$condition),mean)
aggregate(Data300$responseTime,list(Data300$condition),sd)
MICT <- mean(IncongruentTrials$responseTime)
SDICT <- sd(IncongruentTrials$responseTime)
MCT <- mean(CongruentTrials$responseTime)
SDCT <- sd(CongruentTrials$responseTime)
cbind(c(MICT,MCT), c(SDICT,SDCT))
table = cbind(c(MICT,MCT), c(SDICT,SDCT))
colnames(table) = c("mean","standard deviation")
rownames(table) = c("Incongruent", "Congruent")
t.test(IncongruentTrials$responseTime, CongruentTrials$responseTime, paired = T, var.equal = T)
library(ggplot2)
IncongruentNoOutliers <- subset(IncongruentTrials, responseTime < 1267.0660+401.6672*3.5 & responseTime > 1267.0660-401.6672*3.5)
CongruentNoOutliers <- subset(CongruentTrials, responseTime < 1009.4660+352.9656*3.5 & responseTime > 1009.4660-352.9656*3.5)
CongruentNoOutliers$Identify = "Congruent"
IncongruentNoOutliers$Identify = "Incongruent"
AllNoOutliers = rbind(CongruentNoOutliers, IncongruentNoOutliers)
ggplot(AllNoOutliers, aes(x=responseTime, fill=as.factor(identify))) + geom_histogram(alpha = 0.5, position = 'identity') + scale_fill_discrete(name="Trial Type")

【问题讨论】:

  • 您应该能够添加比这更多的代码,除非您超过了问题的最大字符限制(在这种情况下,您应该将问题的范围缩小到合身)。您可以再试一次,但在代码前添加一些常规文本。

标签: r ggplot2


【解决方案1】:

我制作了这个示例代码来说明你的 ggplot 行没问题:

responseTime <- sample(20:50, 200, TRUE)
identify <- sample(letters[1:2], 200, TRUE)

AllNoOutliers <- data.frame(responseTime, identify, stringsAsFactors = FALSE)

library(ggplot2)
ggplot(AllNoOutliers, aes(x=responseTime, fill=as.factor(identify))) + 
    geom_histogram(alpha = 0.5, 
                   position = 'identity', binwidth = 20) + 
    scale_fill_discrete(name="Trial Type") 

检查class(AllNoOutliers$responseTime) 是否为numeric

【讨论】:

  • 好吧,如果它是正确的,怎么会发生错误?它与数字有关吗?
  • 考虑到运行ggplot()后的错误,表示输入错误。如果AllNoOutliers$responseTime 不是数字,你应该先这样做:AllNoOutliers$responseTime &lt;- as.numeric(AllNoOutliers$responseTime)
  • 好的,我试过了,现在它说:“unique.default(x, nmax = nmax) 中的错误:unique() 仅适用于向量”
  • str(AllNoOutliers) 的输出是什么?
  • > str(AllNoOutliers) 类“tbl_df”、“tbl”和“data.frame”:1107 obs。 of 22 variables: $ resultId : chr "57dd6d64067e1e64203ffe98" "57dd6d64067e1e64203ffe98" "57dd6d64067e1e64203ffe98" "57dd6d64067e1e64203ffe98" ... $ participantId : chr "57dd6c91067e1e64203ffe96" "57dd6c91067e1e64203ffe96" "57dd6c91067e1e64203ffe96" "57dd6c91067e1e64203ffe96" ... $ group : chr "none" " none” “none” “none” ... $ 时间戳:POSIXct,格式:“2016-09-17 16:20:52” ... $ frameId : chr “a9bc8be4a2aa8788” “b4d1f8fc16065f14” “9f32552d13229c0b” “9140ee1a93217faf”。 ..(第 1 部分)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-12
相关资源
最近更新 更多