slowkow / ggrepel

:round_pushpin: Repel overlapping text labels away from each other in your ggplot2 figures.
https://ggrepel.slowkow.com
GNU General Public License v3.0
1.21k stars 95 forks source link

Using subsetting in nudge combined with facet_wrap() #138

Open wordsmith189 opened 5 years ago

wordsmith189 commented 5 years ago

Summary

In my chart I can either use subsetting in the nudge_x argument to geom_label_repel(), like this

nudge_x = 1050 - subset(data.means, F2nuc < 1600)$F2nuc,

or I can use facet_wrap() to break out my plot into two panels by one of the group factors. But I can't do both: when I keep both the nudge_x and the facet_wrap in the call to ggplot, things crash.

Minimal code example

Here is the code that does not work because it contains both the facet_wrap and the nudge elements. If I leave either of them out, I at least get a plot - but it'd be great to use both of them.

library(tidyverse)
library(scales)
library(ggthemes)
library(ggrepel)
set.seed(42)

vowel <- c("CHOICE", "DRESS", "FACE", "FLEECE", "FOOT", 
           "GOAT", "GOOSE", "KIT", "LOT/PALM/START", "MOUTH", 
           "NURSE", "PRICE", "STRUT", "THOUGHT", "TRAP/BATH")
vowelclass <- c("long", "short", "long", "long", "short", "long", "long", "short",
                "long", "long", "long", "long", "short", "long", "short")
F1nuc <- c(432.2298, 470.0430, 411.0955, 348.8965, 396.5128, 445.1375, 349.9190,
           401.3571, 521.6027, 482.3714, 398.9484, 500.4809, 472.9490, 477.1944,
           533.7897)
F2nuc <- c(1072.064, 1499.041, 1772.628, 1825.357, 1418.273, 1275.455, 1443.043,
           1611.058, 1249.752, 1502.553, 1365.589, 1419.620, 1347.935, 1171.713,
           1535.666)
df <- data.frame(vowel, vowelclass, F1nuc, F2nuc)

# make the plot
p <- ggplot(data=df, aes(x=F2nuc, y=F1nuc)) +

  scale_x_reverse(name="F2 (Hz)") + 
  scale_y_reverse(name="F1 (Hz)") +

  geom_label_repel(
    data          = subset(df, F2nuc > 1600),
    aes(label=vowel),
    nudge_x       = 2200 - subset(df, F2nuc > 1600)$F2nuc,
    segment.size  = 0.2,
    direction     = "y",
    hjust         = 1
  ) +

  geom_label_repel(
    data          = subset(df, F2nuc < 1600),
    aes(label=vowel),
    nudge_x       = 1050 - subset(df, F2nuc < 1600)$F2nuc,
    segment.size  = 0.2,
    direction     = "y",
    hjust         = 0
  ) +

  geom_point(size=4, color="black") +

    facet_wrap(~ vowelclass)

p + scale_fill_grey() +
  theme_bw() +
  labs(title = "Long and short vowel systems",
       subtitle="Control group",
       caption = (expression(paste(italic("N")[English], " = 20,992"))))
#> Warning in data$x + nudge_x: longer object length is not a multiple of
#> shorter object length
#> Error in data.frame(x = data$x + nudge_x, y = data$y + nudge_y): arguments imply differing number of rows: 3, 2

Created on 2019-06-08 by the reprex package (v0.3.0)

Version information

Here is the output from sessionInfo() in my R session:

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
slowkow commented 4 years ago

Sorry for the delayed response. I got a chance to run your example, and I'm not sure what I am supposed to see.

Could I please ask if you can help me to understand your issue?


library(tidyverse)
library(scales)
#> 
#> Attaching package: 'scales'
#> The following object is masked from 'package:purrr':
#> 
#>     discard
#> The following object is masked from 'package:readr':
#> 
#>     col_factor
library(ggthemes)
library(ggrepel)
set.seed(42)

df <- data.frame(
  vowel = c(
    "CHOICE", "DRESS", "FACE", "FLEECE", "FOOT",
    "GOAT", "GOOSE", "KIT", "LOT/PALM/START", "MOUTH",
    "NURSE", "PRICE", "STRUT", "THOUGHT", "TRAP/BATH"
  ),
  vowelclass = c(
    "long", "short", "long", "long", "short", "long", "long", "short",
    "long", "long", "long", "long", "short", "long", "short"
  ),
  F1nuc = c(
    432.2298, 470.0430, 411.0955, 348.8965, 396.5128, 445.1375, 349.9190,
    401.3571, 521.6027, 482.3714, 398.9484, 500.4809, 472.9490, 477.1944,
    533.7897
  ),
  F2nuc = c(
    1072.064, 1499.041, 1772.628, 1825.357, 1418.273, 1275.455, 1443.043,
    1611.058, 1249.752, 1502.553, 1365.589, 1419.620, 1347.935, 1171.713,
    1535.666
  )
)

# make the plot
ggplot(data = df, aes(x = F2nuc, y = F1nuc)) +

  scale_x_reverse(name = "F2 (Hz)") +
  scale_y_reverse(name = "F1 (Hz)") +

  geom_label_repel(
    data = subset(df, F2nuc > 1600),
    aes(label = vowel),
    nudge_x = 2200 - subset(df, F2nuc > 1600)$F2nuc,
    segment.size = 0.2,
    direction = "y",
    hjust = 1
  ) +

  geom_label_repel(
    data = subset(df, F2nuc < 1600),
    aes(label = vowel),
    nudge_x = 1050 - subset(df, F2nuc < 1600)$F2nuc,
    segment.size = 0.2,
    direction = "y",
    hjust = 0
  ) +

  geom_point(size = 4, color = "black") +

  facet_wrap(~vowelclass) +
  scale_fill_grey() +
  theme_bw() +
  labs(
    title = "Long and short vowel systems",
    subtitle = "Control group",
    caption = (expression(paste(italic("N")[English], " = 20,992")))
  )

Created on 2019-11-03 by the reprex package (v0.3.0)

Session info ``` r devtools::session_info() #> ─ Session info ────────────────────────────────────────────────────────── #> setting value #> version R version 3.6.1 (2019-07-05) #> os macOS Mojave 10.14.6 #> system x86_64, darwin15.6.0 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz America/New_York #> date 2019-11-03 #> #> ─ Packages ────────────────────────────────────────────────────────────── #> package * version date lib #> assertthat 0.2.1 2019-03-21 [1] #> backports 1.1.4 2019-04-10 [1] #> broom 0.5.2 2019-04-07 [1] #> callr 3.3.2 2019-09-22 [1] #> cellranger 1.1.0 2016-07-27 [1] #> cli 1.1.0 2019-03-19 [1] #> colorspace 1.4-1 2019-03-18 [1] #> crayon 1.3.4 2017-09-16 [1] #> curl 4.1 2019-09-16 [1] #> desc 1.2.0 2018-05-01 [1] #> devtools 2.2.0 2019-09-07 [1] #> digest 0.6.21 2019-09-20 [1] #> dplyr * 0.8.3 2019-07-04 [1] #> DT 0.9 2019-09-17 [1] #> ellipsis 0.3.0 2019-09-20 [1] #> evaluate 0.14 2019-05-28 [1] #> forcats * 0.4.0 2019-02-17 [1] #> fs 1.3.1 2019-05-06 [1] #> generics 0.0.2 2018-11-29 [1] #> ggplot2 * 3.2.1.9000 2019-09-27 [1] #> ggrepel * 0.8.0.9000 2019-11-03 [1] #> ggthemes * 4.2.0 2019-05-13 [1] #> glue 1.3.1 2019-03-12 [1] #> gtable 0.3.0 2019-03-25 [1] #> haven 2.1.1 2019-07-04 [1] #> highr 0.8 2019-03-20 [1] #> hms 0.5.1 2019-08-23 [1] #> htmltools 0.3.6 2017-04-28 [1] #> htmlwidgets 1.3 2018-09-30 [1] #> httr 1.4.1 2019-08-05 [1] #> jsonlite 1.6 2018-12-07 [1] #> knitr 1.25 2019-09-18 [1] #> labeling 0.3 2014-08-23 [1] #> lattice 0.20-38 2018-11-04 [1] #> lifecycle 0.1.0 2019-08-01 [1] #> lubridate 1.7.4 2018-04-11 [1] #> magrittr 1.5 2014-11-22 [1] #> memoise 1.1.0 2017-04-21 [1] #> mime 0.7 2019-06-11 [1] #> modelr 0.1.5 2019-08-08 [1] #> munsell 0.5.0 2018-06-12 [1] #> nlme 3.1-141 2019-08-01 [1] #> pillar 1.4.2 2019-06-29 [1] #> pkgbuild 1.0.5 2019-08-26 [1] #> pkgconfig 2.0.3 2019-09-22 [1] #> pkgload 1.0.2 2018-10-29 [1] #> prettyunits 1.0.2 2015-07-13 [1] #> processx 3.4.1 2019-07-18 [1] #> ps 1.3.0 2018-12-21 [1] #> purrr * 0.3.2 2019-03-15 [1] #> R6 2.4.0 2019-02-14 [1] #> Rcpp 1.0.2 2019-07-25 [1] #> readr * 1.3.1 2018-12-21 [1] #> readxl 1.3.1 2019-03-13 [1] #> remotes 2.1.0 2019-06-24 [1] #> rlang 0.4.0 2019-06-25 [1] #> rmarkdown 1.15 2019-08-21 [1] #> rprojroot 1.3-2 2018-01-03 [1] #> rvest 0.3.4 2019-05-15 [1] #> scales * 1.0.0 2018-08-09 [1] #> sessioninfo 1.1.1 2018-11-05 [1] #> stringi 1.4.3 2019-03-12 [1] #> stringr * 1.4.0 2019-02-10 [1] #> testthat 2.2.1 2019-07-25 [1] #> tibble * 2.1.3 2019-06-06 [1] #> tidyr * 1.0.0 2019-09-11 [1] #> tidyselect 0.2.5 2018-10-11 [1] #> tidyverse * 1.2.1 2017-11-14 [1] #> usethis 1.5.1 2019-07-04 [1] #> vctrs 0.2.0 2019-07-05 [1] #> withr 2.1.2 2018-03-15 [1] #> xfun 0.9 2019-08-21 [1] #> xml2 1.2.2 2019-08-09 [1] #> yaml 2.2.0 2018-07-25 [1] #> zeallot 0.1.0 2018-01-28 [1] #> source #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.1) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> Github (tidyverse/ggplot2@23e3241) #> local #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.1) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.1) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> #> [1] /Library/Frameworks/R.framework/Versions/3.6/Resources/library ```