41

Figure

In the figure, is it possible to jitter the state abbreviation labels a bit so they don't overlap? If I use check_overlap = TRUE, then it removes some observations that overlap, and I don't want that. I also don't want the geom_label_repel, since it has the labels stick out and move across the 45 degree line I included (which I don't want to happen)

Here's the pertinent part of my code for reference:

ggplot(df, aes(x = huff_margin_dem, y = margin16dem_state, label = abbrev)) +
  geom_abline(intercept = 0) +
  geom_text(fontface = "bold")
Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
  • 1
    Without [reproducible code](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) it is impossible to help you directly. However, you should check out `ggrepel` more closely to look for control of the label movement. – Mark Peterson Nov 11 '16 at 17:37

2 Answers2

44

Have you tried position=position_jitter()? You can adjust the width and height to your choosing.

ggplot(df, aes(x = huff_margin_dem, y = margin16dem_state, label = abbrev)) +
  geom_abline(intercept = 0) +
  geom_text(fontface = "bold",position=position_jitter(width=1,height=1))

EDIT An example to manipulate a certain label only

+geom_text(fontface = "bold",
position=position_jitter(width=ifelse(df$abbrev=='KS',1,0),
      height=ifelse(df$abbrev=='KS',1,0)))

Or multiple labels

df$jit<-with(df, ifelse(abbrev == "KS" | abbrev == "LA", 1, 2))

+geom_text(fontface = "bold",
    position=position_jitter(width=df$jit,height=df$jit))
J.Con
  • 4,101
  • 4
  • 36
  • 64
  • Thank you for this! Do you happen to know if there's a way to jitter specific texts and not just all of them? For example, in the above image example, is there a way to jitter the observations with the "KS" and "LA" text but not the other observations? – Alexander Agadjanian Nov 12 '16 at 03:46
43

Just thought I'd point out that ggrepel::geom_text_repel will do what you're after. Considering some of the text in your example already overlaps with the line, I figure perhaps it is the label part of geom_label_repel that you don't like, due to the background it will place behind your text, blocking the line.

Using your example:

ggplot(df) +
  geom_text_repel(aes(x = huff_margin_dem, 
                      y = margin16dem_state, 
                      label = abbrev))
conor
  • 1,204
  • 1
  • 18
  • 22
  • 1
    Agreed! Although it might be more understandable with the aesthetics within ggplot() like the other examples: ggplot(df, aes(x = huff_margin_dem, y = margin16dem_state, label = abbrev)) + geom_text_repel() – Billy Raseman Mar 14 '19 at 22:39
  • 3
    In my experimentation, ggrepel::geom_text_repel does a superior job of neatly presenting non-overlapping labels than manipulating the arguments of position=position_jitter(), the latter appearing in a previous answer. – Rick Pack Aug 14 '19 at 21:45
  • Works really well. thumbs up!! – NullPumpkinException Jan 04 '23 at 08:16