使用ggstatsplot在离散变量和数字变量之间转换时出错

时间:2019-11-21 17:13:31

标签: r variables ggplot2 continuous

我正在使用ggstatsplot()创建一些图形。但是,我遇到了一个变量问题:我不断收到“提供给连续刻度的离散值”错误,但我不知道为什么。

我尝试了as.numeric并创建了新的变量,将因数转换回数字和双精度,但是没有任何效果。

使用常规ggplot2()时不会出现相同的错误。

我想知道是否有人可以帮助我找出问题所在?

这是一个完整的可复制示例,其中包含我正在使用的确切数据。

#here's the exact data I am working with.
df <- structure(list(condition = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("0", 
"1", "2", "3"), class = "factor"), size = c(3, 1, 1, 2, 5, 4, 
5, 3, 1, 1, 4, 3, 5, 4, 4, 4, 2, 2, 4, 5, 3, 3, 3, 3, 5, 1, 5, 
5, 5, 1, 3, 4, 2, 1, 2, 1, 1, 3, 3, 1, 2, 3, 1, 4, 5, 5, 1, 5, 
4, 5, 5, 1, 1, 4, 1, 2, 5, 1, 2, 2, 5, 3, 3, 4, 5, 3, 3, 3, 2, 
1, 2, 4, 1, 1, 4, 4, 1, 2, NA, 3, 1, 4, 4, 2, 3, 4, 4, 4, 3, 
5, 4, 2, 2, 5, 5, 5, 4, 1, 2, 5, 5), predict = c(4, 4, 1, 1, 
1, 4, 2, 4, 3, 2, 2, 3, 1, 1, 4, 3, 5, 2, 4, 2, 1, 5, 3, 3, 3, 
3, 4, 2, 1, 1, 5, 2, 5, 3, 3, 3, 1, 5, 2, 3, 5, 2, 2, 5, 3, 2, 
1, 4, 2, 2, 4, 4, 1, 4, 3, 3, 1, 1, 2, 3, 4, 4, 2, 5, 4, 3, 2, 
3, 4, 4, 5, 2, 2, 4, 2, 2, 5, 4, NA, 1, 2, 3, 3, 5, 5, 5, 5, 
1, 1, 1, 2, 1, 4, 2, 1, 2, 5, 3, 1, 4, 5), meaningful = c(6, 
5, 3, 3, 5, 4, 3, 2, 4, 6, 6, 4, 2, 2, 4, 5, 2, 5, 2, 4, 5, 1, 
2, 7, 5, 7, 6, 3, 4, 4, 3, 7, 2, 2, 2, 4, 3, 3, 1, 6, 7, 1, 5, 
1, 7, 4, 1, 2, 3, 4, 1, 1, 4, 1, 7, 3, 4, 7, 6, 6, 2, 5, 5, 6, 
4, 3, 5, 6, 4, 1, 1, 2, 1, 4, 7, 5, 4, 6, NA, 5, 5, 6, 7, 4, 
3, 7, 7, 5, 4, 3, 1, 5, 5, 1, 6, 1, 5, 2, 5, 2, 1)), row.names = c(NA, 
-101L), class = c("tbl_df", "tbl", "data.frame"))

#here's some code I'm using to generate plots.
#if you need to, 
#install.packages("ggstatsplot")

library(ggstatsplot)
ggbetweenstats(data = df,
  x = condition, y = size,
  point.jitter.height = .2,
  plot.type = "violin",
  type = "p",
  effsize.type = "partial_eta",
  xlab = "Effect size condition",
  ylab = "Perceived Size",
  bf.message = F
  ) 

ggbetweenstats(data = df,
  x = condition, y = predict,
  point.jitter.height = .2,
  plot.type = "violin",
  type = "p",
  effsize.type = "partial_eta",
  xlab = "Effect size condition",
  ylab = "Prediction",
  bf.message = F
  ) 

#here's where I get the error
ggbetweenstats(data = df,
  x = condition, y = meaningful,
  point.jitter.height = .2,
  plot.type = "violin",
  type = "p",
  effsize.type = "partial_eta",
  xlab = "Effect size condition",
  ylab = "Meaningfulness",
  bf.message = F
  ) 

#I don't get the error using regular old ggplot, though.
library(ggplot2)
ggplot(data = df, aes(x = condition, y = meaningful)) + 
  geom_violin(aes(fill = condition)) + 
  geom_boxplot(width = .1, coef = 0) + 
  geom_jitter(width = .1, alpha = .5) + 
  theme_classic()

1 个答案:

答案 0 :(得分:1)

这是ggstatsplot中的错误。变量名称为模式"mean"的任何数据框(如此处的meaningful都受此问题的影响。

现在是fixed

# setup
set.seed(123)
library(ggstatsplot)

# data
df <- structure(list(condition = structure(c(
  1L, 1L, 1L, 1L, 1L, 1L,
  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
  2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
  2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
  3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
  3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
  4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L
), .Label = c(
  "0",
  "1", "2", "3"
), class = "factor"), size = c(
  3, 1, 1, 2, 5, 4,
  5, 3, 1, 1, 4, 3, 5, 4, 4, 4, 2, 2, 4, 5, 3, 3, 3, 3, 5, 1, 5,
  5, 5, 1, 3, 4, 2, 1, 2, 1, 1, 3, 3, 1, 2, 3, 1, 4, 5, 5, 1, 5,
  4, 5, 5, 1, 1, 4, 1, 2, 5, 1, 2, 2, 5, 3, 3, 4, 5, 3, 3, 3, 2,
  1, 2, 4, 1, 1, 4, 4, 1, 2, NA, 3, 1, 4, 4, 2, 3, 4, 4, 4, 3,
  5, 4, 2, 2, 5, 5, 5, 4, 1, 2, 5, 5
), predict = c(
  4, 4, 1, 1,
  1, 4, 2, 4, 3, 2, 2, 3, 1, 1, 4, 3, 5, 2, 4, 2, 1, 5, 3, 3, 3,
  3, 4, 2, 1, 1, 5, 2, 5, 3, 3, 3, 1, 5, 2, 3, 5, 2, 2, 5, 3, 2,
  1, 4, 2, 2, 4, 4, 1, 4, 3, 3, 1, 1, 2, 3, 4, 4, 2, 5, 4, 3, 2,
  3, 4, 4, 5, 2, 2, 4, 2, 2, 5, 4, NA, 1, 2, 3, 3, 5, 5, 5, 5,
  1, 1, 1, 2, 1, 4, 2, 1, 2, 5, 3, 1, 4, 5
), meaningful = c(
  6,
  5, 3, 3, 5, 4, 3, 2, 4, 6, 6, 4, 2, 2, 4, 5, 2, 5, 2, 4, 5, 1,
  2, 7, 5, 7, 6, 3, 4, 4, 3, 7, 2, 2, 2, 4, 3, 3, 1, 6, 7, 1, 5,
  1, 7, 4, 1, 2, 3, 4, 1, 1, 4, 1, 7, 3, 4, 7, 6, 6, 2, 5, 5, 6,
  4, 3, 5, 6, 4, 1, 1, 2, 1, 4, 7, 5, 4, 6, NA, 5, 5, 6, 7, 4,
  3, 7, 7, 5, 4, 3, 1, 5, 5, 1, 6, 1, 5, 2, 5, 2, 1
)), row.names = c(
  NA,
  -101L
), class = c("tbl_df", "tbl", "data.frame"))

# plot
ggbetweenstats(
  data = df,
  x = condition, 
  y = meaningful
)
#> Note: 95% CI for effect size estimate was computed with 100 bootstrap samples.
#> 
#> Note: Shapiro-Wilk Normality Test for meaningful: p-value = < 0.001
#> 
#> Note: Bartlett's test for homogeneity of variances for factor condition: p-value = 0.284
#> 

reprex package(v0.3.0)于2019-11-22创建