const-ae / ggsignif

Easily add significance brackets to your ggplots
https://const-ae.github.io/ggsignif/
GNU General Public License v3.0
592 stars 43 forks source link

Can't add p values to stacked bar plot #50

Closed yanxianl closed 6 years ago

yanxianl commented 6 years ago

Hi,

First of all, thank you for developing and maintaining the ggsignif !

I was trying to add significant p values to a faceted stacked bar plot but kept getting an error message saying

Error in check_factor(f) : object 'Rank' not found

Below are the data and code to reproduce my problem:

library(tidyverse) 
#> Warning: 程辑包'tidyverse'是用R版本3.4.4 来建造的
#> Warning: 程辑包'ggplot2'是用R版本3.4.4 来建造的
#> Warning: 程辑包'tibble'是用R版本3.4.4 来建造的
#> Warning: 程辑包'tidyr'是用R版本3.4.4 来建造的
#> Warning: 程辑包'readr'是用R版本3.4.4 来建造的
#> Warning: 程辑包'purrr'是用R版本3.4.4 来建造的
#> Warning: 程辑包'dplyr'是用R版本3.4.4 来建造的
#> Warning: 程辑包'stringr'是用R版本3.4.4 来建造的
#> Warning: 程辑包'forcats'是用R版本3.4.4 来建造的
library(cowplot) 
#> Warning: 程辑包'cowplot'是用R版本3.4.4 来建造的
#> 
#> 载入程辑包:'cowplot'
#> The following object is masked from 'package:ggplot2':
#> 
#>     ggsave
library(ggsignif) 
#> Warning: 程辑包'ggsignif'是用R版本3.4.4 来建造的

# Make a dataframe for plotting stacked bar plot
df <- data.frame(Diet = rep(c("REF", "IM"), each = 8),
                 Variable = c("hpv", "hpv", "hpv", "hpv", "smc", "smc", "lpc", "lpc",
                              "hpv", "hpv", "hpv", "smc", "smc", "smc", "lpc", "lpc"),
                 Rank = c("Mild", "Moderate", "Marked", "Severe", "Normal", "Mild", "Normal", "Mild",
                          "Mild", "Moderate", "Marked", "Normal", "Mild", "Moderate", "Normal", "Mild"),
                 Percent = c(5.56, 38.9, 44.4, 11.1, 38.9, 61.1, 77.8, 22.2, 
                             16.7, 66.7, 16.7, 11.1, 72.2, 16.7, 50, 50)
                 )

# Specify the desired orders of factors and convert "Rank" to an ordered factor
df$Diet <- factor(df$Diet, levels = c("REF", "IM"))
df$Variable <- factor(df$Variable, levels = c("hpv", "smc", "lpc"))
df$Rank <- ordered(df$Rank, levels = c("Normal", "Mild", "Moderate", "Marked", "Severe")) # Rank as ordered factor

# Define color scheme 
my_col = c(Normal = "royalblue2", Mild = "peachpuff1", Moderate = "tan1", Marked = "tomato", Severe = "red3")

# Make stacked barplot 
p <- ggplot(df, aes(Diet, Percent, fill = forcats::fct_rev(Rank))) + # forcats::fct_rev() reverses stacked bars
  geom_bar(stat = "identity") +
  facet_wrap(~ Variable, nrow = 1) +
  scale_fill_manual(values = my_col) + 
  scale_y_continuous(limits = c(0, 105), breaks = 0:5*20, expand = expand_scale(mult = c(0, 0.05))) +
  labs(title = "Stacked bar plot", y = "%") +
  guides(fill = guide_legend(title = "Rank")) + 
  theme_cowplot()

# Make a datafraome for p value annotation
anno <- data.frame(Variable = "hpv",
                   p = 0.03,
                   start = "REF",
                   end = "IM",
                   y = 102)

# Add p value to the plot
p + geom_signif(data = anno,
                aes(xmin = start, 
                    xmax = end, 
                    annotations = p, 
                    y_position = y),
                textsize = 4, 
                tip_length = 0,
                manual = TRUE)
#> Warning: Ignoring unknown aesthetics: xmin, xmax, annotations, y_position
#> Error in check_factor(f): 找不到对象'Rank'


# P values cann't be added. Even when I tried to add p value manually using geom_text + geom_segment
# P values can be added if the barplots are not stacked by Rank

Created on 2018-11-05 by the reprex package (v0.2.1)

Session info ``` r devtools::session_info() #> - Session info ---------------------------------------------------------- #> setting value #> version R version 3.4.3 (2017-11-30) #> os Windows 10 x64 #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate Chinese (Simplified)_China.936 #> ctype Chinese (Simplified)_China.936 #> tz Europe/Berlin #> date 2018-11-05 #> #> - Packages -------------------------------------------------------------- #> package * version date lib source #> assertthat 0.2.0 2017-04-11 [1] CRAN (R 3.4.4) #> backports 1.1.2 2017-12-13 [1] CRAN (R 3.4.3) #> base64enc 0.1-3 2015-07-28 [1] CRAN (R 3.4.1) #> bindr 0.1.1 2018-03-13 [1] CRAN (R 3.4.4) #> bindrcpp 0.2.2 2018-03-29 [1] CRAN (R 3.4.4) #> broom 0.5.0 2018-07-17 [1] CRAN (R 3.4.3) #> callr 3.0.0 2018-08-24 [1] CRAN (R 3.4.4) #> cellranger 1.1.0 2016-07-27 [1] CRAN (R 3.4.4) #> cli 1.0.1 2018-09-25 [1] CRAN (R 3.4.4) #> colorspace 1.3-2 2016-12-14 [1] CRAN (R 3.4.4) #> cowplot * 0.9.3 2018-07-15 [1] CRAN (R 3.4.4) #> crayon 1.3.4 2017-09-16 [1] CRAN (R 3.4.4) #> curl 3.2 2018-03-28 [1] CRAN (R 3.4.4) #> debugme 1.1.0 2017-10-22 [1] CRAN (R 3.4.4) #> desc 1.2.0 2018-05-01 [1] CRAN (R 3.4.4) #> devtools 2.0.0 2018-10-19 [1] CRAN (R 3.4.3) #> digest 0.6.18 2018-10-10 [1] CRAN (R 3.4.4) #> dplyr * 0.7.7 2018-10-16 [1] CRAN (R 3.4.4) #> evaluate 0.12 2018-10-09 [1] CRAN (R 3.4.4) #> forcats * 0.3.0 2018-02-19 [1] CRAN (R 3.4.4) #> fs 1.2.6 2018-08-23 [1] CRAN (R 3.4.4) #> ggplot2 * 3.1.0 2018-10-25 [1] CRAN (R 3.4.4) #> ggsignif * 0.4.0 2017-08-03 [1] CRAN (R 3.4.4) #> glue 1.3.0 2018-07-17 [1] CRAN (R 3.4.4) #> gtable 0.2.0 2016-02-26 [1] CRAN (R 3.4.4) #> haven 1.1.2 2018-06-27 [1] CRAN (R 3.4.4) #> hms 0.4.2 2018-03-10 [1] CRAN (R 3.4.4) #> htmltools 0.3.6 2017-04-28 [1] CRAN (R 3.4.4) #> httr 1.3.1 2017-08-20 [1] CRAN (R 3.4.4) #> jsonlite 1.5 2017-06-01 [1] CRAN (R 3.4.4) #> knitr 1.20 2018-02-20 [1] CRAN (R 3.4.4) #> lattice 0.20-35 2017-03-25 [2] CRAN (R 3.4.3) #> lazyeval 0.2.1 2017-10-29 [1] CRAN (R 3.4.4) #> lubridate 1.7.4 2018-04-11 [1] CRAN (R 3.4.4) #> magrittr 1.5 2014-11-22 [1] CRAN (R 3.4.4) #> memoise 1.1.0 2017-04-21 [1] CRAN (R 3.4.4) #> mime 0.6 2018-10-05 [1] CRAN (R 3.4.4) #> modelr 0.1.2 2018-05-11 [1] CRAN (R 3.4.4) #> munsell 0.5.0 2018-06-12 [1] CRAN (R 3.4.4) #> nlme 3.1-137 2018-04-07 [1] CRAN (R 3.4.4) #> pillar 1.3.0 2018-07-14 [1] CRAN (R 3.4.4) #> pkgbuild 1.0.2 2018-10-16 [1] CRAN (R 3.4.3) #> pkgconfig 2.0.2 2018-08-16 [1] CRAN (R 3.4.4) #> pkgload 1.0.1 2018-10-11 [1] CRAN (R 3.4.4) #> plyr 1.8.4 2016-06-08 [1] CRAN (R 3.4.4) #> prettyunits 1.0.2 2015-07-13 [1] CRAN (R 3.4.4) #> processx 3.2.0 2018-08-16 [1] CRAN (R 3.4.4) #> ps 1.1.0 2018-08-10 [1] CRAN (R 3.4.4) #> purrr * 0.2.5 2018-05-29 [1] CRAN (R 3.4.4) #> R6 2.3.0 2018-10-04 [1] CRAN (R 3.4.4) #> Rcpp 0.12.19 2018-10-01 [1] CRAN (R 3.4.4) #> readr * 1.1.1 2017-05-16 [1] CRAN (R 3.4.4) #> readxl 1.1.0 2018-04-20 [1] CRAN (R 3.4.4) #> remotes 2.0.1 2018-10-19 [1] CRAN (R 3.4.3) #> rlang 0.2.2 2018-08-16 [1] CRAN (R 3.4.4) #> rmarkdown 1.10 2018-06-11 [1] CRAN (R 3.4.4) #> rprojroot 1.3-2 2018-01-03 [1] CRAN (R 3.4.4) #> rvest 0.3.2 2016-06-17 [1] CRAN (R 3.4.4) #> scales 1.0.0 2018-08-09 [1] CRAN (R 3.4.4) #> sessioninfo 1.1.0 2018-09-25 [1] CRAN (R 3.4.4) #> stringi 1.1.7 2018-03-12 [1] CRAN (R 3.4.4) #> stringr * 1.3.1 2018-05-10 [1] CRAN (R 3.4.4) #> testthat 2.0.1 2018-10-13 [1] CRAN (R 3.4.4) #> tibble * 1.4.2 2018-01-22 [1] CRAN (R 3.4.4) #> tidyr * 0.8.1 2018-05-18 [1] CRAN (R 3.4.4) #> tidyselect 0.2.5 2018-10-11 [1] CRAN (R 3.4.4) #> tidyverse * 1.2.1 2017-11-14 [1] CRAN (R 3.4.4) #> usethis 1.4.0 2018-08-14 [1] CRAN (R 3.4.4) #> withr 2.1.2 2018-03-15 [1] CRAN (R 3.4.4) #> xml2 1.2.0 2018-01-24 [1] CRAN (R 3.4.4) #> yaml 2.2.0 2018-07-25 [1] CRAN (R 3.4.4) #> #> [1] C:/Users/ljt89/Documents/R/win-library/3.4 #> [2] C:/Program Files/R/R-3.4.3/library ```
yanxianl commented 6 years ago

Hi,

I just fixed the problem by moving the aes(fill = forcats::fct_rev(Rank)) from the global aesthetics to the local aesthetics in geom_bar(). Namely:

ggplot(df, aes(Diet, Percent)) + geom_bar(aes(fill = forcats::fct_rev(Rank)), stat = "identity") # forcats::fct_rev() reverses stacked bars

Cheers, Yanxian