Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Axis label formatting fails when position is changed #5

Closed
joelnitta opened this issue Sep 29, 2019 · 5 comments
Closed

Axis label formatting fails when position is changed #5

joelnitta opened this issue Sep 29, 2019 · 5 comments

Comments

@joelnitta
Copy link

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
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

This comment has been minimized.

@clauswilke
Copy link
Collaborator

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
Copy link
Author

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
Copy link
Collaborator

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
Copy link
Author

I see, thanks for taking the time to explain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants