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

标签 r variables ggplot2 continuous

我正在使用 ggstatsplot() 创建一些图表。但是,我遇到了一个变量的问题:我不断收到“提供给连续刻度的离散值”错误,我无法弄清楚原因。

我已经尝试过 as.numeric 并创建新变量,将 factor 转换回 numeric 和 double,但没有任何效果。

当我使用常规的 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()

最佳答案

这是 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 创建于 2019-11-22 (v0.3.0)

关于r - 使用 ggstatsplot 在离散变量和数值变量之间转换时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58980300/

相关文章:

python - 关于python变量作用域的一个小问题

r - R align.time/aggregate 中的错误?

PHP如何将eval(0分配给变量

variables - 如何找到SSIS包中所有使用变量的地方?

r - 在stat_summary_hex中,如果z是一个因数,为什么六边形重叠?

r - 使用 Shiny 的 R-markdown 创建动态图

r - R 中的直方图和密度

r - 将字符转换为数字而不丢失信息

r - 对相同的列表元素进行分组并计算其在 R 中的出现次数

r - ggplot2: "geom_point requires the following missing aesthetics: x, y"