wilkelab / ggtext

Improved text rendering support for ggplot2
https://wilkelab.org/ggtext/
GNU General Public License v2.0
655 stars 37 forks source link

Axis label formatting fails when position is changed #5

Closed joelnitta closed 4 years ago

joelnitta commented 4 years ago

Thanks for the great package!

I would like to format labels and move the label position using the position argument of scale_x etc, but it doesn't seem to work.

library(ggplot2)
library(ggtext)

labels <- c(
  setosa = "This is<br>*I. setosa*",
  virginica = "This is<br>*I. virginica*",
  versicolor = "This is<br>*I. versicolor*"
)

# Works with labels on bottom
ggplot(iris, aes(Species, Sepal.Width)) +
  geom_boxplot() +
  scale_x_discrete(
    name = NULL,
    labels = labels
  ) +
  theme(
    axis.text.x = element_markdown(color = "black", size = 11)
  )


# But not on top
ggplot(iris, aes(Species, Sepal.Width)) +
  geom_boxplot() +
  scale_x_discrete(
    position = "top",
    name = NULL,
    labels = labels
  ) +
  theme(
    axis.text.x = element_markdown(color = "black", size = 11)
  )

Created on 2019-09-29 by the reprex package (v0.3.0)

Session info ``` r sessionInfo() #> R version 3.6.0 (2019-04-26) #> Platform: x86_64-apple-darwin15.6.0 (64-bit) #> Running under: macOS High Sierra 10.13.6 #> #> Matrix products: default #> BLAS: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib #> LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib #> #> locale: #> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 #> #> attached base packages: #> [1] stats graphics grDevices utils datasets methods base #> #> other attached packages: #> [1] ggtext_0.1.0 ggplot2_3.2.1 #> #> loaded via a namespace (and not attached): #> [1] Rcpp_1.0.2 pillar_1.4.2 compiler_3.6.0 highr_0.8 #> [5] tools_3.6.0 digest_0.6.21 evaluate_0.14 tibble_2.1.3 #> [9] gtable_0.3.0 pkgconfig_2.0.3 rlang_0.4.0 curl_4.2 #> [13] yaml_2.2.0 xfun_0.9 withr_2.1.2 dplyr_0.8.3 #> [17] stringr_1.4.0 httr_1.4.1 knitr_1.25 xml2_1.2.2 #> [21] grid_3.6.0 tidyselect_0.2.5 gridtext_0.1.0 glue_1.3.1 #> [25] R6_2.4.0 rmarkdown_1.15 purrr_0.3.2 magrittr_1.5 #> [29] scales_1.0.0 htmltools_0.3.6 assertthat_0.2.1 mime_0.7 #> [33] colorspace_1.4-1 labeling_0.3 stringi_1.4.3 lazyeval_0.2.2 #> [37] munsell_0.5.0 markdown_1.1 crayon_1.3.4 ```
clauswilke commented 4 years ago

This is a problem with how ggplot2 is rendering the top labels. Nothing I can do here to address this. However, it's good to collect these cases, as eventually they'll have to be addressed in ggplot2.

clauswilke commented 4 years ago

This actually works. You have to style the correct theme element.

library(ggplot2)
library(ggtext)

labels <- c(
  setosa = "This is<br>*I. setosa*",
  virginica = "This is<br>*I. virginica*",
  versicolor = "This is<br>*I. versicolor*"
)

ggplot(iris, aes(Species, Sepal.Width)) +
  geom_boxplot() +
  scale_x_discrete(
    position = "top",
    name = NULL,
    labels = labels
  ) +
  theme(
    axis.text.x.top = element_markdown(color = "black", size = 11)
  )

Created on 2019-10-07 by the reprex package (v0.3.0)

joelnitta commented 4 years ago

I see, thanks for pointing that out! Looking at the ggplot2 reference, "axis.title.*.* inherits from axis.title.*", so does that mean in this case ggtext is not behaving as expected?

clauswilke commented 4 years ago

No, it behaves as expected. axis.text.x.top is still an element_text(), so it'll behave like one. Inheritance only means that the parameter values are reused. See the following example.

library(ggplot2)
library(ggtext)

labels <- c(
  setosa = "This is<br>*I. setosa*",
  virginica = "This is<br>*I. virginica*",
  versicolor = "This is<br>*I. versicolor*"
)

# example 1, parameters are inherited but no markdown
ggplot(iris, aes(Species, Sepal.Width)) +
  geom_boxplot() +
  scale_x_discrete(
    position = "top",
    name = NULL,
    labels = labels
  ) +
  theme(
    axis.text.x = element_markdown(color = "red", size = 15)
  )


# example 2, now with markdown
ggplot(iris, aes(Species, Sepal.Width)) +
  geom_boxplot() +
  scale_x_discrete(
    position = "top",
    name = NULL,
    labels = labels
  ) +
  theme(
    axis.text.x = element_text(color = "red", size = 15),
    axis.text.x.top = element_markdown()
  )

Created on 2019-10-07 by the reprex package (v0.3.0)

joelnitta commented 4 years ago

I see, thanks for taking the time to explain.