5

I want to put labels inside the plot area.

Here's an example plot:

library(ggplot2)

set.seed(123)
random_word <- function() paste(sample(letters, 10, replace = T), collapse='')
dat <- data.frame(x = replicate(4, random_word()),
                  y = runif(5*4, 0, 100))

ggplot(dat, aes(x = x, y = y)) +
  geom_point() +
  coord_flip() +
  theme_minimal()

enter image description here

I know I can use geom_text to hack my way to the result I want:

ggplot(dat, aes(x = x, y = y)) +
  geom_point() +
  geom_text(data = dat[1:4,],
            aes(label=x), 
            y = 1, 
            hjust=0, vjust=-1 ) +
  coord_flip() +
  theme_minimal() +
  theme(axis.text.y = element_blank())

enter image description here

Comments on this question [ move axis labels ggplot ] suggest that margin is the non hacky way to do this in current ggplot, but it doesn't seem to move the axis text inside the plot area.

# doesn't do the trick
ggplot(dat, aes(x = x, y = y)) +
  geom_point() +
  coord_flip() +
  theme_minimal() +
  theme(axis.title.y = element_text(margin = margin(l = 50)))

Is there some elegant way to get this sort of output by setting theme() parameters?

my sessionInfo() is:

> sessionInfo()
R version 3.5.2 (2018-12-20)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.3

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] ggthemes_4.1.0  forcats_0.4.0   stringr_1.4.0   dplyr_0.8.0.1   purrr_0.3.1     readr_1.3.1    
 [7] tidyr_0.8.3     tibble_2.0.1    ggplot2_3.1.0   tidyverse_1.2.1
arvi1000
  • 9,393
  • 2
  • 42
  • 52
  • First of all, you want to move the `axis.text.y` so when you test that comment and it doesn't work, actually you are not even changing the right element, but that's not the main thing. `margin()` is obviously changing the margins. Labels are still getting printed in the area of between axis line and plot border so margin won't work for bringing labels inside the plot. Unless there's an option like what we have for plotting at the top/bottom/etc. (e.g. `scale_x_discrete(position = "inside")` ), which I am not aware of, I guess your best bet is your *hacky* solution. – M-- Mar 28 '19 at 22:20

1 Answers1

1

Interesting question. I'm not aware of an elegant way of doing this, but margin() does move the text into the plot area if you really want it to. In order for it to work, you also need to play around with the right parameter instead of just the left parameter.

I tried playing around with the top and bottom parameters as well, but it seems that margin() indeed doesn't do anything here. However, you can use vjust for vertically shifting it away from the grid lines.

Also note that I corrected your error where you shifted the axis title instead of the axis text.

ggplot(dat, aes(x = x, y = y)) +
geom_point() +
coord_flip() +
theme_minimal() +
theme(axis.text.y = element_text(vjust = -0.7,
                                 margin = margin(l = 20, r = -50))) # note the negative value for r

see here for output

PRZ
  • 551
  • 2
  • 11