修改由 ggplot 和 ggpubr 创建的嵌套列表中的对象?

问题描述 投票:0回答:2

考虑以下 MWE,其中在虚构的研究中测量了虚构的青少年参与者的身高:

library(dplyr)
library(ggplot2)
library(ggpubr)

# Create data for height, measured at two time points, for imaginary participants
participant_id <- 1:100
`Time point 1` <- rnorm(100, mean = 162, sd = 7)
`Time point 2` <- rnorm(100, mean = 165, sd = 7)
heights <- tibble(participant_id, `Time point 1`, `Time point 2`)

# View the head of the data
head(heights, n=3)
#> # A tibble: 3 × 3
#>   participant_id `Time point 1` `Time point 2`
#>            <int>          <dbl>          <dbl>
#> 1              1           165.           176.
#> 2              2           168.           175.
#> 3              3           176.           173.

# Stack the data to long format for ggplot
heights_long <- stack(heights[,c(2, 3)])

# View the head and tail of the data
rbind(head(heights_long, n=2), tail(heights_long, n=2))
#>       values          ind
#> 1   164.6773 Time point 1
#> 2   167.7354 Time point 1
#> 199 159.0590 Time point 2
#> 200 158.5412 Time point 2

# Create a ggplot object
bp <- ggplot(heights_long, aes(x=ind, y=values, fill=ind)) +
  ggtitle("Height measured at two time points") +
  geom_boxplot() +                                 
  theme(legend.position = "none") +                
  labs(x = "Time point of measurement") +          
  labs(y = "Height (cm)")

# Add mean comparison p-values with stat_compare_means
p1 <- bp + stat_compare_means(
  method = "t.test",
  label = "p.format",
  show.legend = FALSE,
  paired = TRUE,
  comparisons = list(c("Time point 1", "Time point 2")),
  method.args = list(symbol = TRUE)
)

# Display the ready graph
p1

  
# Introduce a custom function for rounding a p-value
custom_round <- function(x) {
  case_when(
    x >= 0 && x < 0.01 ~ "p <.01",
    TRUE ~ paste0("p = ", format(round(x, 2), nsmall = 2))
  )
}

创建于 2023-04-27 与 reprex v2.0.2

现在的问题是:如果我想使用上述自定义函数对图表中显示的 p 值(此处:0.041)进行样式化

custom_round

我可以直接在对象“p1”中访问该“字段”吗?还是我必须先通过 ggplot_built 将其放入,然后尝试应用该函数,然后才可能尝试显示修改后的新图形对象?

r ggplot2 nested-lists ggpubr
2个回答
2
投票

我认为你正在寻找的东西可以通过

symnum.args
stat_compare_means
参数来实现。

library(dplyr)
library(ggplot2)
library(ggpubr)

# Create data for height, measured at two time points, for imaginary participants
set.seed(200)
participant_id <- 1:100
`Time point 1` <- rnorm(100, mean = 162, sd = 7)
`Time point 2` <- rnorm(100, mean = 165, sd = 7)
heights <- tibble(participant_id, `Time point 1`, `Time point 2`)

# Stack the data to long format for ggplot
heights_long <- stack(heights[,c(2, 3)])

# Create a ggplot object
bp <- ggplot(heights_long, aes(x=ind, y=values, fill=ind)) +
  ggtitle("Height measured at two time points") +
  geom_boxplot() +                                 
  theme(legend.position = "none") +                
  labs(x = "Time point of measurement") +          
  labs(y = "Height (cm)")


# Set symnum argument
symnum_args <- list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, Inf), symbols = c("p <= 0.0001", "p <= 0.001","p <= .01", "p <= .05", "ns"))

# Add mean comparison p-values with stat_compare_means
p1 <- bp + stat_compare_means(
  method = "t.test",
  label = "p.format",
  show.legend = FALSE,
  paired = TRUE,
  comparisons = list(c("Time point 1", "Time point 2")),
  method.args = list(symbol = TRUE)#,
  #symnum.args = symnum_args
)

p2 <- bp + stat_compare_means(
  method = "t.test",
  label = "p.format",
  show.legend = FALSE,
  paired = TRUE,
  comparisons = list(c("Time point 1", "Time point 2")),
  method.args = list(symbol = TRUE),
  symnum.args = symnum_args
)

# Display the ready graph
p1

p2

创建于 2023-04-27 与 reprex v2.0.2


0
投票

我能够自己从elsewhere找到关于如何直接访问ggplot对象中的数据的说明。

解决办法见下。这使用户可以完全控制要显示的内容,并且比

stat_compare_means()
.

的“内置”symnum_args 参数更强大,恕我直言
library(dplyr)
library(ggplot2)
library(ggpubr)

# Create data for height, measured at two time points, for imaginary participants
participant_id <- 1:100
set.seed(123)
`Time point 1` <- rnorm(100, mean = 162, sd = 7)
`Time point 2` <- rnorm(100, mean = 165, sd = 7)
heights <- tibble(participant_id, `Time point 1`, `Time point 2`)

# Stack the data to long format for ggplot
heights_long <- stack(heights[,c(2, 3)])

# Create a ggplot object
bp <- ggplot(heights_long, aes(x=ind, y=values, fill=ind)) +
  ggtitle("Height measured at two time points") +
  geom_boxplot() +                                 
  theme(legend.position = "none") +                
  labs(x = "Time point of measurement") +          
  labs(y = "Height (cm)")

# Add mean comparison p-values with stat_compare_means
p1 <- bp + stat_compare_means(
  method = "t.test",
  label = "p.format",
  show.legend = FALSE,
  paired = TRUE,
  comparisons = list(c("Time point 1", "Time point 2")),
  method.args = list(symbol = TRUE)
)

# Display the ready graph
p1


# Introduce a custom function for rounding a p-value
custom_round <- function(x) {
  case_when(
    x >= 0 & x < 0.01 ~ "p < .01",
    TRUE ~ paste0("p = ", format(round(x, 2), nsmall = 2))
  )
}

## Extract data with ggplot_build
p1build <- ggplot_build(p1)

## Explore data in p1build to find the right variable (here the command is commented)
#p1build$data

# In this case, the p-value could very easily be found in: p4build$data[[2]]$annotation

# Turn p-value "back" to numeric, then apply the function
p_rounded <- p1build$data[[2]]$annotation %>% as.numeric() %>% custom_round()

# Replace the original p-value with the rounded p-value
p1build$data[[2]]$annotation <- p_rounded

# Plot anew
plot(ggplot_gtable(p1build))

创建于 2023-04-29 与 reprex v2.0.2

© www.soinside.com 2019 - 2024. All rights reserved.