ggtreeExtra icon indicating copy to clipboard operation
ggtreeExtra copied to clipboard

Log scale

Open RhettRautsaw opened this issue 4 years ago • 4 comments

Is there a way to log scale a geom_fruit facet without simultaneously log scaling the underlying phylogeny?

RhettRautsaw avatar Dec 03 '21 22:12 RhettRautsaw

The geom_fruit layer and the tree layer are on the same panel. the original scales x functions of ggplot2 are not compatible with geom_fruit. I think we might need to develop some similar scales functions for the x axis of geom_fruit layer. Now, I think you can use the log in the aes function, and you can refer to the example below.

library(ggplot2)
library(ggtree)
library(ggtreeExtra)

set.seed(123)
tr <- rtree(100)
dd = data.frame(id=tr$tip.label, value=abs(rnorm(100)))
p <- ggtree(tr)
p1 <- p + 
geom_fruit(
   data = dd,
   geom = geom_col,
   mapping = aes(x = log10(value), y=id),
   orientation = "y",
   offset = 0.5,
   pwidth = 0.5,
   grid.params=list(), 
   axis.params=list(axis="x", text.size = 2)
)
p1

xx

xiangpin avatar Dec 06 '21 02:12 xiangpin

Maybe slightly in line with this question: Does this also mean that it is not possible to manually set the axis limits of geom_fruit layers? I looked into xlim_expand function (package: ggtree) but I was unable to find an equivalent to "panel"-name of this function.

jseidel89 avatar May 23 '22 16:05 jseidel89

Yes, the axis limits of geom_fruit can not be set manually now. The grid line and axis of geom_fruit are the line layers generated by ggplot2, which are not the coord of ggplot2.

xiangpin avatar May 25 '22 11:05 xiangpin

Is there a workaround for this particular issue, especially with regards to using log-scales on data that you also want to present means and errors on (like here)? I trying to show levels of gene expression across a gene family using means + SE, but this needs a log scale because of variation between genes. Using geom_fruit_list I can either (1) generate a plot with the means and SE but many values are too small to see, (2) generate a plot with the means pre-log transformed before plotting but then the SE values are not properly mapped because they cannot come from log transformations of the calculated SE, or (3) pre-transform the raw data and plot it but then the scale is too small to see? I have attached some screenshots to demonstrate all three issues in order below:

Screenshot 2024-01-26 at 2 58 16 PM Screenshot 2024-01-26 at 2 58 27 PM Screenshot 2024-01-26 at 2 58 57 PM

NikoHensley avatar Jan 26 '24 15:01 NikoHensley