hughjonesd / ggmagnify

Create a magnified inset of part of a ggplot object
https://hughjonesd.github.io/ggmagnify/
Other
272 stars 3 forks source link

Format y axis #25

Closed FriederikeHanssen closed 5 months ago

FriederikeHanssen commented 6 months ago

Hey!

Thanks for the awesome package. I was trying to format the y-axis labels, which works in the main plot but seems to be ignored in the magnified portion. Is it possible to do this? If so what would be the right way?

Describe the bug A clear and concise description of what the bug is.

To Reproduce

cost_table_ncbi <- readxl::read_excel('data/Sequencing_Cost_Data_Table_May2022.xls')

cost_table_ncbi$Date <- as.Date(cost_table_ncbi$Date)

# Define a custom formatter function
dollar_format <- function(x) {
  #ifelse(x >= 1e6, paste0("$", round(x / 1e6, 2), "mio"), paste0("$", comma(x)))
  paste0("$", comma(x))
}

ggplot(data = cost_table_ncbi, aes(x = Date)) +
  geom_line(aes(y = `Cost per Genome`), color = "blue") +
  coord_cartesian(clip = "off") + 
  ggtitle("Cost for sequencing a single human whole genome") +
  theme(axis.title.y = element_blank(),
        plot.title = element_text(face = "bold"))+
  scale_y_continuous(labels = dollar_format) +
  geom_magnify(from=list(ymd("2012-01-31"), ymd("2022-10-31"), 0 , 1e4),
               to=list(ymd("2008-05-31"), ymd("2023-5-31"),2.5e7, 8.5e7 ),
               shadow = TRUE, axes = "xy", recompute = TRUE, expand =0,
               after_scale = list(scale_y_continuous = dollar_format))

Sequencing_Cost_Data_Table_May2022.xls

Expected behavior I would like the yaxis labels in the magnified plot to also have $ prepended (among other things)

Screenshots

Screenshot 2024-03-15 at 16 23 09

Other information scales lubridate ggmagnify ggpubr ggplot2 "1.3.0" "1.9.3" "0.4.0" "0.6.0" "3.5.0"

hughjonesd commented 6 months ago

I'm travelling right now but I'd expect you could solve this via the plot argument. Is there any reason to expect passing after_scale to geom_magnify to work?

hughjonesd commented 5 months ago

If you're still interested, I'll need a reproducible example, i.e. one that runs when I copy/paste it.

hughjonesd commented 5 months ago

The following example shows the problem, which only happens when recompute = TRUE:

library(ggplot2)

ggp <- ggplot(iris, aes(Sepal.Width, Sepal.Length, color = Species, shape = Species))
ggp2 <- ggp + geom_point()

from <- c(2.5, 3.0, 5.5, 6.0)
to <- c(3.5, 4.5, 4.5, 5.5)

ggp2 + 
  scale_y_continuous(labels = scales::label_currency()) + 
  geom_magnify(from = from, to = to, axes = "xy") # dollars shown on y axis

ggp2 + 
  scale_y_continuous(labels = scales::label_currency()) + 
  geom_magnify(from = from, to = to, axes = "xy", recompute = TRUE) # dollars not shown
hughjonesd commented 5 months ago

My thought would be, you probably don't need recompute = TRUE here. Adding recompute = TRUE adds a call to lims(); this is documented, but it does wipe out your existing scale. The other solution is to use the plot argument to specify exactly what you want the inset plot to be.