teunbrand / ggh4x

ggplot extension: options for tailored facets, multiple colourscales and miscellaneous
https://teunbrand.github.io/ggh4x/
Other
534 stars 32 forks source link

Cannot use `trans` after using `facetted_pos_scales` #137

Closed Yunuuuu closed 6 months ago

Yunuuuu commented 7 months ago

I have created a pcakge to use ggplot2 in Complexheatmap deposited in https://github.com/Yunuuuu/eheat. The internal use ggh4x to create limits for each panel, everything works well except the trans cannot work as expected. I don't know how to deal with it. See following examples

reverse trans:

knitr::opts_knit$set(upload.fun = identity)
library(ggh4x)
#> Loading required package: ggplot2
library(ggplot2)
ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
  geom_point(aes(colour = Species)) +
  geom_text(aes(label = Species)) +
  scale_x_reverse() +
  facet_grid(rows = vars(Species), scales = "free_y") +
  facetted_pos_scales(y = list(
    ggplot2::scale_y_continuous(expand = ggplot2::expansion()),
    ggplot2::scale_y_continuous(expand = ggplot2::expansion()),
    ggplot2::scale_y_continuous(expand = ggplot2::expansion())
  ))
#> Warning: Removed 150 rows containing missing values (`geom_point()`).
#> Warning: Removed 150 rows containing missing values (`geom_text()`).

image

log trans:

knitr::opts_knit$set(upload.fun = identity)
library(ggh4x)
#> Loading required package: ggplot2
library(ggplot2)
ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
  geom_point(aes(colour = Species)) +
  geom_text(aes(label = Species)) +
  scale_x_continuous(trans = "log10") +
  facet_grid(rows = vars(Species), scales = "free_y") +
  facetted_pos_scales(y = list(
    ggplot2::scale_y_continuous(expand = ggplot2::expansion()),
    ggplot2::scale_y_continuous(expand = ggplot2::expansion()),
    ggplot2::scale_y_continuous(expand = ggplot2::expansion())
  ))
#> Warning: Removed 150 rows containing missing values (`geom_point()`).
#> Warning: Removed 150 rows containing missing values (`geom_text()`).

image

Created on 2023-11-24 with reprex v2.0.2

Standard output and standard error ``` sh -- nothing to show -- ```

~

teunbrand commented 7 months ago

Thanks for the report! I agree with you that his behaviour is suboptimal but I don't have a quick fix for this. As a workaround, you might consider providing the transformed scale to facetted_pos_scales() as well, though that'd throw a (harmless) warning.

library(ggh4x)
#> Loading required package: ggplot2
library(ggplot2)
ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
  geom_point(aes(colour = Species)) +
  geom_text(aes(label = Species)) +
  facet_grid(rows = vars(Species), scales = "free_y") +
  facetted_pos_scales(y = list(
    ggplot2::scale_y_continuous(expand = ggplot2::expansion()),
    ggplot2::scale_y_continuous(expand = ggplot2::expansion()),
    ggplot2::scale_y_continuous(expand = ggplot2::expansion())
  ), x = scale_x_reverse())
#> Warning: Attempting to add facetted x scales, while x scales are not free.
#> ℹ Try adding `scales = "free_x"` to the facet.

Created on 2023-11-23 with reprex v2.0.2

Yunuuuu commented 7 months ago

Thank you for your response. That sounds like a good workaround. I can check if the user has provided an axis scale and then add it into the facetted_pos_scales for further processing. I'll try.

Yunuuuu commented 7 months ago

This works well, the axis of both bottom and left annotation has been transformed. Thanks for your help

library(ggplot2)
library(ComplexHeatmap)
library(eheat)

set.seed(123)
nr1 <- 4
nr2 <- 8
nr3 <- 6
nr <- nr1 + nr2 + nr3
nc1 <- 6
nc2 <- 8
nc3 <- 10
nc <- nc1 + nc2 + nc3
mat <- cbind(
  rbind(
    matrix(rnorm(nr1 * nc1, mean = 1, sd = 0.5), nr = nr1),
    matrix(rnorm(nr2 * nc1, mean = 0, sd = 0.5), nr = nr2),
    matrix(rnorm(nr3 * nc1, mean = 0, sd = 0.5), nr = nr3)
  ),
  rbind(
    matrix(rnorm(nr1 * nc2, mean = 0, sd = 0.5), nr = nr1),
    matrix(rnorm(nr2 * nc2, mean = 1, sd = 0.5), nr = nr2),
    matrix(rnorm(nr3 * nc2, mean = 0, sd = 0.5), nr = nr3)
  ),
  rbind(
    matrix(rnorm(nr1 * nc3, mean = 0.5, sd = 0.5), nr = nr1),
    matrix(rnorm(nr2 * nc3, mean = 0.5, sd = 0.5), nr = nr2),
    matrix(rnorm(nr3 * nc3, mean = 1, sd = 0.5), nr = nr3)
  )
)
mat <- mat[sample(nr, nr), sample(nc, nc)] # random shuffle rows and columns
rownames(mat) <- paste0("row", seq_len(nr))
colnames(mat) <- paste0("column", seq_len(nc))
small_mat <- mat[1:9, 1:9]

anno_data <- sample(1:10, nrow(small_mat))
# library(eheat)
draw(ggheat(small_mat,
  top_annotation = HeatmapAnnotation(
    foo = gganno(
      matrix = anno_data,
      function(p) {
        p + aes(y = V1) + geom_text(aes(label = .index))
      },
      which = "column", height = unit(2, "cm")
    ), which = "column"
  ),
  bottom_annotation = HeatmapAnnotation(
    foo = gganno(
      function(p) {
        p + aes(y = V1) +
          geom_text(aes(label = .index)) +
          scale_y_continuous(limits = rev)
      },
      matrix = anno_data,
      which = "column", height = unit(2, "cm")
    ),
    which = "column"
  ),
  right_annotation = HeatmapAnnotation(
    foo = gganno(
      function(p) {
        p + aes(x = V1) +
          geom_text(aes(label = .index))
      },
      matrix = anno_data,
      which = "row", width = unit(3, "cm")
    ),
    which = "row"
  ),
  left_annotation = HeatmapAnnotation(
    foo = gganno(
      function(p) {
        p + aes(x = V1) +
          geom_text(aes(label = .index)) +
          scale_x_continuous(limits = rev)
      },
      matrix = anno_data,
      which = "row", width = unit(3, "cm")
    ),
    which = "row"
  ),
  row_km = 2L, column_km = 2L,
), merge_legends = TRUE)

image