Aligning ggrepel labels

扶醉桌前 提交于 2020-07-20 03:46:05

问题


I have a script that downloads a bunch of COVID data about the area surrounding some Air Force bases and produces a chart. I was going to try to reproduce it using a big dput() but it's too many characters for a post. The dput() to reproduce the data frame can be copied from here.

The data itself looks like

head(smDailyBaseData)

DaysSince Base    abbv          newRate label         
  <drtn>    <chr>   <chr>           <dbl> <chr>         
1  7 days   Edwards EdwardsAFB 0          "Edwards \n 0"
2  8 days   Edwards EdwardsAFB 0.00000140 "Edwards \n 2"
3  9 days   Edwards EdwardsAFB 0.00000140 "Edwards \n 0"
4 10 days   Edwards EdwardsAFB 0.00000140 "Edwards \n 0"
5 11 days   Edwards EdwardsAFB 0.00000140 "Edwards \n 0"
6 12 days   Edwards EdwardsAFB 0.00000140 "Edwards \n 0"

for 737 rows...

the plot code is

#endpoint layer
BaseEndpoints <- smDailyBaseData %>% 
  group_by(Base) %>%
  filter(DaysSince == max(DaysSince)) %>%
  select(Base, abbv, DaysSince, newRate,label) %>%
  ungroup()

ZoomEndpoints <- BaseEndpoints %>% filter(Base != 'Hanscom') %>%
  mutate(zoom = TRUE)

MAEndPoint <- BaseEndpoints %>% filter(Base == 'Hanscom') %>%
  mutate(zoom = FALSE)

ZoomEndpoints <- rbind(ZoomEndpoints, MAEndPoint)

BasePlot <- smDailyBaseData %>% 
  ggplot(mapping = aes(x = as.numeric(DaysSince), y = newRate)) +
  geom_line(aes(color=abbv),show.legend = FALSE) +
  scale_color_ucscgb() +
  geom_point(data = BaseEndpoints,size = 1.5,shape = 21, 
             aes(color = abbv,fill = abbv), show.legend = FALSE) +
  geom_label_repel(data=ZoomEndpoints, aes(label=label), show.legend = FALSE,
                   vjust = 0) +
  labs(x = "Days Since First Confirmed Case", 
       y = "% Local Population Infected Daily") +
  theme(plot.title = element_text(size = rel(1), face = "bold"),
        plot.subtitle = element_text(size = rel(0.7)),
        plot.caption = element_text(size = rel(1))) +

  facet_zoom(xlim = c(50,130), ylim=c(0,0.0075),zoom.data=zoom)



print(BasePlot)

The result is this plot

which is still kind of crowded despite the zoom, with labels covering lots of data. I'd like to get all the labels lined up on the right hand side of the zoom plot, where all the blank space is (say, above 120 on the x-axis), and then they can just have longer lines to connect to the endpoints. But I haven't been able to achieve that. I've tried wetting direction to y and using hjust as described here but don't get the distance or the alignment I'm looking for.

Any guidance would be appreciated.


回答1:


You're looking for the xlim= and ylim= arguments in geom_label_repel(). They work the same way as axis limits, but they are the limits for the "repelled" labels. You can play around a bit, but here's the result after changing that line to read:

geom_label_repel(data=ZoomEndpoints, aes(label=label), show.legend = FALSE,
                   vjust = 0, xlim=c(100,120), size=3, direction = 'y')

Your mileage may vary, since the output depends on graphics device resolution and aspect ratio.



来源:https://stackoverflow.com/questions/62225251/aligning-ggrepel-labels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!