teunbrand / ggh4x

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

ggtext::element_markdown not working with ggh4x_0.2.8 #153

Closed flohump closed 3 months ago

flohump commented 3 months ago

The following plotting script worked fine with ggh4x_0.2.7, ggplot2_3.4.2 and ggtext_0.1.2. See figure ggplot2_OK.png.

However, with the most recent versions from CRAN, ggh4x_0.2.8, ggplot2_3.5.0 and ggtext_0.1.2, I get an error: Don't know how to get width of ‹integer› object. See ggplot2_Problem2.png. This is caused by ggtext::element_markdown(). If replaced with element_text, the plot is produced. But of the course the html code in df$bundle is not interpreted.

With ggh4x_0.2.7, ggplot2_3.5.0 and ggtext_0.1.2, the plot is produced but size = "variable" in strip_nested has no effect. See ggplot2_Problem1.png.

It would be awesome if these bugs could be fixed.

library(ggtext)
library(ggh4x)
library(ggplot2)

#install.packages("https://cran.r-project.org/src/contrib/Archive/ggplot2/ggplot2_3.5.0.tar.gz",repos = NULL,type="source")
#install.packages("https://cran.r-project.org/src/contrib/Archive/ggh4x/ggh4x_0.2.7.tar.gz",repos = NULL,type="source")

df <- data.frame(bundleName = c("Diet","Diet"),
                 bundle = rep(c("<span style = 'color:blue;'>Scen1</span>, <span style = 'color:red;'>Scen2</span>"),2),
                 vargroup = c("Environment","Environment"),
                 variable = c("CO2 emissions","CO2 emissions"), 
                 scenCol = c("blue","red"),
                 valuefill = c(5, 10))

p <- ggplot(df, aes(x = get("valuefill"), y = get("bundleName"))) + ggtitle("ggh4x_0.2.8 ggplot2_3.5.0 ggtext_0.1.2") + 
  theme_minimal() + theme(panel.border = element_rect(colour = NA, fill = NA)) +
  facet_nested(get("bundle") ~ get("vargroup") + get("variable"), scales = "free_y", space = "free_y", switch = "y",
               strip = strip_nested(size = "variable", 
                                    text_x = elem_list_text(angle = c(0, 90), face=c("bold","plain")), 
                                    text_y = ggtext::element_markdown(angle = 0),
                                    by_layer_x = TRUE)) +
  geom_bar(data = df, mapping = aes(fill = get("scenCol")),position = position_stack(reverse = TRUE), stat = "identity", width = 0.45, show.legend = FALSE) +
  scale_fill_manual("Scenario", values = c("blue","red")) +
  labs(y = NULL, x = NULL) + 
  theme(plot.background = element_rect(fill = "white"), strip.background = element_rect(color = "grey50"),
        axis.line = element_blank(), axis.ticks = element_blank(), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), axis.text.x.bottom = element_blank())+
  theme(axis.text.y = element_text(angle = 90, vjust = 0.5, hjust = 0.5, face = "bold"))

ggsave("ggplot2_Problem2.png",p,height = 5,width = 10, scale = 0.5)

ggplot2_OK.png ggplot2_OK

ggplot2_Problem2.png

ggplot2_Problem2

ggplot2_Problem1.png ggplot2_Problem1

teunbrand commented 3 months ago

Hi there, thanks for the report! Is this the same as #151 and does installing the github version solve the problem?

flohump commented 3 months ago

Yes, indeed. It is the same issue. Using the most recent GitHub version via devtools::install_github("teunbrand/ggh4x") solves both problems. The plot ggplot2_OK.png can be reproduced. Thanks for your help!

Are you planning a new release of ggh4x soon?

teunbrand commented 3 months ago

Are you planning a new release of ggh4x soon?

Not really, I haven't accumulated enough bugfixes or improvements to bother CRAN with a new release just yet. I'll mark this issue as complete as the issue is the same as the previous one.