Shrilaxmi M S
Shrilaxmi M S

Reputation: 171

Error can't find the y.position variable 'y.position' in the data

I am trying to plot significant values in ggplot boxplot I am getting error unable understand why this error is comming

my data

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))

code I used

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")

Can anyone suggest me to resolve the issue

Upvotes: 3

Views: 4436

Answers (4)

nicolas
nicolas

Reputation: 1

My solution (for ggplots using geom_bar):

#inherit.aes = FALSE to break link to previous aesthetics

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))

Got inspired here: https://statisticsglobe.com/r-ggplot2-geom_path-error-fun-object-not-found

Upvotes: 0

LDT
LDT

Reputation: 3088

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")

enter image description here

Upvotes: 2

danlooo
danlooo

Reputation: 10627

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")

Created on 2021-09-13 by the reprex package (v2.0.1)

Upvotes: 6

Saskia Schirmer
Saskia Schirmer

Reputation: 171

It will work out, when you specify the y.position before plotting (see in the examples of ?stat_pvalue_manual):

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

Just adjust the positions to your data.

Upvotes: 2

Related Questions