R语言 错误:在数据中找不到y.position变量'y.position'

tnkciper  于 2022-12-20  发布在  其他
关注(0)|答案(4)|浏览(303)

我尝试在ggplot箱线图中绘制重要值,我收到错误,无法理解为什么会出现此错误
我的数据

structure(list(data1 = c(0.05, 0.068, 0.063, 0.075, 0.047), 
      data2 = c(-0.029,-0.011,0.009,0.117,0.116), 
      data3 = c(-0.048,-0.030,-0.026,-0.049,-0.087), 
      data4 = c(-0.070,-0.072,-0.035,-0.001,-0.021)), 
      class = "data.frame", row.names = c(NA, -5L))

我使用的代码

library(reshape)
library(ggplot2)
library(ggpubr)

regcovMat <- read.table("Test.txt", sep="\t", header=T)
molten.data <- melt(regcovMat)
stat.test <- compare_means(
  value ~ variable, data = molten.data,
  method = "t.test"
)

data=ggplot(molten.data, aes(x=variable, y=value, fill=variable)) + 
  geom_boxplot()+theme_cowplot()+theme(legend.position = "top")
data + stat_pvalue_manual(stat.test, label = "p")

有人能建议我解决这个问题吗

uelo1irk

uelo1irk1#

library(reshape)
library(ggplot2)
library(ggpubr)
library(cowplot)
#> 
#> Attaching package: 'cowplot'
#> The following object is masked from 'package:ggpubr':
#> 
#>     get_legend
#> The following object is masked from 'package:reshape':
#> 
#>     stamp

regcovMat <- structure(list(data1 = c(0.05, 0.068, 0.063, 0.075, 0.047), data2 = c(-0.029, -0.011, 0.009, 0.117, 0.116), data3 = c(-0.048, -0.030, -0.026, -0.049, -0.087), data4 = c(-0.070, -0.072, -0.035, -0.001, -0.021)), class = "data.frame", row.names = c(NA, -5L))

molten.data <- melt(regcovMat)
#> Using  as id variables
stat.test <- compare_means(
  value ~ variable,
  data = molten.data,
  method = "t.test"
)

ggplot(molten.data) +
  geom_boxplot(aes(x = variable, y = value, fill = variable)) +
  stat_pvalue_manual(stat.test, label = "p", y.position = 0.15, step.increase = 0.2) +
  theme_cowplot() +
  theme(legend.position = "top")

reprex package(v2.0.1)于2021年9月13日创建

ogsagwnx

ogsagwnx2#

check <- structure(list(data1 = c(0.05, 0.068, 0.063, 0.075, 0.047), 
               data2 = c(-0.029,-0.011,0.009,0.117,0.116), 
               data3 = c(-0.048,-0.030,-0.026,-0.049,-0.087), 
               data4 = c(-0.070,-0.072,-0.035,-0.001,-0.021)), 
          class = "data.frame", row.names = c(NA, -5L))

check2 <- check %>% 
  pivot_longer(., cols = everything()) 

stat.test <-  compare_means(
    value ~ name, data =check2,
    method = "t.test")
stat.test =stat.test[1,]

ggplot(check2) +
  geom_boxplot(aes(x = name, y = value, fill = name)) +
  stat_pvalue_manual(stat.test, label = "p", y.position = 0.15, step.increase = 0.2) +
  theme_cowplot() +
  scale_fill_viridis(option="plasma",discrete = T) +
  theme(legend.position = "top")

zpgglvta

zpgglvta3#

当您在绘图前指定y.位置时,它将起作用(参见?stat_pvalue_manual的示例):

stat.test <- stat.test %>%
  mutate(y.position = c(0.15,0.1,0.05,0,0.05,0))

根据数据调整位置即可。

thtygnil

thtygnil4#

我的解决方案(对于使用geom_bar的ggplots):

inherit.aes = FALSE以断开与之前美学的链接

ggplot(df, aes(x = x,
               y = y)
  
  geom_bar() +
  
  stat_pvalue_manual(inherit.aes = FALSE, #inherit.aes = FALSE
                     stat.test,
                     y.position = c(1,2,3))

灵感来自:https://statisticsglobe.com/r-ggplot2-geom_path-error-fun-object-not-found

相关问题