Proper way to pass vectorized input to element_text?
Asked Answered
R

1

5

I often find myself wanting to align the x-axis labels on my plots differently based on their location, with values on the left left-aligned and values on the right, right-aligned. However, element_text doesn't have official support for vectorized input (and never will?) so I'm wondering how to solve this in proper ggplot2 fashion.

The problem: overlapping x-axis labels (and truncated on the far right)

library(ggplot2)
v <- data.frame(x=1:2, y=1:2, facet=gl(2, 2))
gp <- ggplot(v) +
  geom_point(aes(x=x, y=y)) +
  scale_x_continuous(breaks = 1:2, labels = c("Minimum", "Maximum")) +
  facet_wrap(~facet)
gp

enter image description here

My solution:

gp + theme(axis.text.x = element_text(hjust=c(0, 1)))

enter image description here

Except:

Warning message:
Vectorized input to `element_text()` is not officially supported.
ℹ Results may be unexpected or may change in future versions of ggplot2. 

What's the correct way to differently-align text in a ggplot if vectorized element_text isn't supported?

Repudiate answered 11/4, 2023 at 0:27 Comment(0)
C
6

Claus Wilke states here that his ggtext package does support the use of vectors in this way and it will continue supporting it in the future. As thomasp85 (Thomas Lin Pedersen) says in the thread "FWIW, I think most of the use cases for this hack can be solved properly using element_markdown()", i.e.

library(ggplot2)
library(ggtext)

v <- data.frame(x=1:2, y=1:2, facet=gl(2, 2))
gp <- ggplot(v) +
  geom_point(aes(x=x, y=y)) +
  scale_x_continuous(breaks = 1:2, labels = c("Minimum", "Maximum")) +
  facet_wrap(~facet)
gp

gp +
  theme(axis.text.x = element_markdown(hjust = c(0, 1)))

Created on 2023-04-11 with reprex v2.0.2

Calamity answered 11/4, 2023 at 1:47 Comment(4)
Hmmm I was hoping for something that didn't require loading an additional library. I'm curious about the ggplot2 crew's vision for how this should go. Is it just a bad idea in general because the labels are offset from the ticks? It seems like it's just a technical problem, so is it worth opening a feature request on the GitHub even though they already seem aware this is an edge case?Repudiate
Great point; I don't think it's a bad idea, it's just not a 'standard' use-case. Based on the discussion thread on github the ggtext code is designed to support vectorised inputs and the ggplot2 code isn't (and they plan to deprecate this 'hack' for element_text() in the future) so it might be worth posting a feature request and see what they say.Calamity
After doing some more searching, it looks like the concerns I have were already raised and considered not worth implementing - this issue says basically the same things I would and the "official" response seems to be that this isn't going to happen and we'll just have to keep fingers crossed that ggtext sticks around. Thanks for your help though!Repudiate
@Repudiate Honestly I'm also a bit concerned that even the ggtext approach is buggy: github.com/wilkelab/ggtext/issues/116#issue-2295783078 The fact that ggtext has been on version 0.10 since 2019 gives me pause.Mendicant

© 2022 - 2024 — McMap. All rights reserved.