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

feature request (maybe from another package) #39

Open
higgi13425 opened this issue Jun 5, 2020 · 0 comments
Open

feature request (maybe from another package) #39

higgi13425 opened this issue Jun 5, 2020 · 0 comments

Comments

@higgi13425
Copy link

higgi13425 commented Jun 5, 2020

Ideally, to have a function able to insert into geom_textbox()
small summary tibbles (2-3 columns by 2-6 rows)
and to have the textbox sized to the appropriate width and height
(using information from the initial tibble)

related to, but a bit fancier than, current issue #36

# Example (can likely be done much more elegantly)

# build a simple table 
iris %>% 
  count(Species) %>% 
  select(n, Species)->
table

# glue together text into a single variable
# ideally would auto-extract colnames to do this, rather than manual gluing
table %>% 
  mutate(text_f = glue("{n} {Species}<br>")) ->
table_f

# count the maximum width in characters
# subtract 4 for "<br>"
char_wide <- max(nchar(table_f$text_f)) - 4

# collapse text to a single text string with linebreaks (except for the last line)
label <- glue_collapse(table_f$text_f) %>% 
  str_extract(pattern = "^.+(?=<br>$)")

# set up locations of text boxes
df <- tibble( label = label,  
              x = c(0.2, .6), 
              y = c(0.8, .5))

# plot text boxes, with width in cm related to character count (plus one for spaces)
ggplot(df) +
  aes(
    x, y, label = label,
    hjust = c(0.5,0.5), vjust = c(0.5,0.5)
    ) +
    geom_textbox(width = unit((char_wide)*0.2, "cm")) +
    xlim(0, 1) + ylim(0, 1)
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

1 participant