2

Hopefully someone can help me with this problem. I´ve tried several solutions with no success.

I want to replicate following figure #1. Therefor I´ve added the variable clock (with values from 1 to 12) to my data frame to use it as parameter for the alignment of geom_text.

Figure I want to replicate:

figure i want to replicate

My figure:

my figure

Following my code for plotting:

p1 <- ggplot(data, aes(x=tax, y=employment)) + geom_point(colour = ifelse(style == 1 | style == 2,"black","grey")) + geom_smooth(method=lm, se=FALSE)
p1 + coord_cartesian(xlim = c(0, 0.8), ylim = c(0.5,0.9), expand = FALSE) + labs(x = "1 - participation taxrate", y="Employment rate") + geom_text(aes(family = "Times New Roman", label=ifelse(style == 1 | style == 2 ,Country,''))) + theme_bw()
DimaSan
  • 12,264
  • 11
  • 65
  • 75
Kaizen502
  • 35
  • 1
  • 6
  • would you be happy with more legible labels (ie less/no overlap) or do you want to remake your example figure exactly? – Nate Oct 17 '16 at 10:53
  • @NathanDay if it's possible i would like to position the text according to my clock variable. Otherwise I'll try to make it more legible with ggrepel. Or do you have a easier solution? – Kaizen502 Oct 17 '16 at 11:04
  • you can use `position = position_nudge()` with appropriate values for x and y. – shadow Oct 17 '16 at 11:08
  • i would fall back to `ggrepel` myself. Looks like @shadow might might be onto something, I was going to try the same idea with hjust and vjust – Nate Oct 17 '16 at 11:13

1 Answers1

1

If you have specified the clock value, you can easily calculate the direction and therefore the amount you want to nudge in each direction:

angle <- clock/12*2*pi
radius <- 0.01
ng <- data.frame(x = radius * sin(angle), 
                 y = radius * cos(angle))

Then you can just add position = position_nudge(x = ng$x, y = ng$y) to the geom_text statement. Below I have also removed the grid lines that don't appear in the figure you are trying to replicate.

p1 + coord_cartesian(xlim = c(0, 0.8), ylim = c(0.5,0.9), expand = FALSE) + 
  labs(x = "1 - participation taxrate", y="Employment rate") + 
  geom_text(aes(family = "Times New Roman", label=ifelse(style == 1 | style == 2 ,Country,'')), 
            position = position_nudge(x = ng$x, y = ng$y)) + 
  theme_bw() +
  theme(panel.grid.major.x = element_blank(), 
        panel.grid.minor.x = element_blank())
shadow
  • 21,823
  • 4
  • 63
  • 77