Skip to content

Tag positioning not working? #2882

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

Closed
anhttdang opened this issue Sep 5, 2018 · 11 comments
Closed

Tag positioning not working? #2882

anhttdang opened this issue Sep 5, 2018 · 11 comments

Comments

@anhttdang
Copy link

Hello,

I used hjust and vjust for plot.tag but it didn't seem to work. Is it an intended behavior? How can I move the tag away from the plot region especially when placing it on the right?

Thank you!

library(dplyr)
library(ggplot2)

mpg <- mpg %>% 
  filter(cyl == 4 | cyl == 8)

p <- ggplot(mpg, aes(displ, cty)) + 
  geom_point() +
  facet_grid(cols = vars(cyl)) +
  labs(tag = 'A)') +
  theme_minimal() +
  theme(plot.caption = element_text(size = 14)) +
  theme(plot.margin = margin(t = 20, r = 20, b = 20, l = 20))

p1 <- p +
  labs(caption = "top") +
  theme(plot.tag.position = 'top')

p2 <- p +
  labs(caption = "top, vjust = 1, hjust = 1") +
  theme(plot.tag.position = 'top',
        plot.tag = element_text(vjust = 1, hjust = 1))

p3 <- p +
  labs(caption = "right") +
  theme(plot.tag.position = 'right')

p4 <- p +
  labs(caption = "right, vjust = 1, hjust = 1") +
  theme(plot.tag.position = 'right',
        plot.tag = element_text(vjust = 1, hjust = 1))

library(patchwork)
(p1 | p2)

(p1 | p3) / (p2 | p4)

@batpigandme
Copy link
Contributor

Using plot.tag.position alone seems to work:

library(dplyr)
library(ggplot2)

mpg <- mpg %>% 
  filter(cyl == 4 | cyl == 8)

p <- ggplot(mpg, aes(displ, cty)) + 
  geom_point() +
  facet_grid(cols = vars(cyl)) +
  labs(tag = 'A)') +
  theme_minimal() +
  theme(plot.caption = element_text(size = 14)) +
  theme(plot.margin = margin(t = 20, r = 20, b = 20, l = 20))

p + theme(plot.tag.position = "topright")

p + theme(plot.tag.position = "bottomleft")

Created on 2018-09-05 by the reprex package (v0.2.0.9000).

@anhttdang
Copy link
Author

@batpigandme thank you for checking! What I meant was that can we move the tag around after we specify its position? For example: moving the tag away or up/down the right border of the plot when use plot.tag.position to make it look better

library(dplyr)
library(ggplot2)

mpg <- mpg %>% 
  filter(cyl == 4 | cyl == 8)

p <- ggplot(mpg, aes(displ, cty)) + 
  geom_point() +
  facet_grid(cols = vars(cyl)) +
  labs(tag = 'A)') +
  theme_bw() +
  theme(plot.caption = element_text(size = 14)) +
  theme(plot.margin = margin(t = 20, r = 20, b = 20, l = 20))
p3 <- p +
  labs(caption = "right") +
  theme(plot.tag.position = 'right')
p3

@clauswilke
Copy link
Member

Apparently not. I suspect this is a bug that can be fixed. In the mean time, you can specify the tag position via a two-dimensional numerical vector, just like the legend position, and that should allow you to move the tag anywhere you want.

@batpigandme
Copy link
Contributor

Ah! OK. I see what you mean now.

Yeah, this was something that came up in the PR thread (@clauswilke just beat me to it, as I was finding his old comment):
#2405 (comment)

@anhttdang
Copy link
Author

@clauswilke : could you post a working example? I couldn't get it to work for plot.tag.position = 'right'. Thank you!

library(dplyr)
library(ggplot2)

mpg <- mpg %>% 
  filter(cyl == 4 | cyl == 8)

p <- ggplot(mpg, aes(displ, cty)) + 
  geom_point() +
  facet_grid(cols = vars(cyl)) +
  labs(tag = 'A)') +
  theme_bw() +
  theme(plot.caption = element_text(size = 14)) +
  theme(plot.margin = margin(t = 20, r = 20, b = 20, l = 20))

p3 <- p +
  labs(caption = "right") 

p3 + theme(plot.tag.position = c(0.5, 0.5))

p3 + theme(plot.tag.position = c(1.1, 0.75))

p3 + theme(plot.tag.position = c(8, 25))

@clauswilke
Copy link
Member

The numbers have to be between 0 and 1, just like for legend.position.

@anhttdang
Copy link
Author

@clauswilke : wouldn't it place the tag inside the facet with the value being from 0 to 1?

@clauswilke
Copy link
Member

I'm worried that the main point of this issue is getting lost, so I'm showing it here in a minimal reprex:

library(ggplot2)

p <- ggplot() + labs(tag = "A") +
  theme(plot.tag.position = "right")

# vjust setting cannot be used to move the
# label vertically by a meaningful amount
p + theme(plot.tag = element_text(vjust = 0))

p + theme(plot.tag = element_text(vjust = 1))

Created on 2018-09-06 by the reprex package (v0.2.0).

Similar problems exist for tags at the top or bottom of a plot and the hjust parameter.

@anhttdang
Copy link
Author

Is there any update on this issue? Thank you!

@thomasp85
Copy link
Member

you can use the margin argument in element_text() for this:

p3 <- p +
  labs(caption = "right") +
  theme(
    plot.tag.position = 'right', 
    plot.tag = element_text(margin = margin(l = 12))
  )

@lock
Copy link

lock bot commented Oct 8, 2019

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Oct 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants