对齐ggrepel标签 [英] Aligning ggrepel labels

查看:170
本文介绍了对齐ggrepel标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,可以下载一些空军基地周围区域的COVID数据,并生成图表.我打算尝试使用大dput()来重现它,但是帖子中的字符太多.可以从此处复制dput()来复制数据帧.

数据本身看起来像

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"

用于737行...

情节代码为

#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)

结果是这个情节

尽管缩放,但仍然有些拥挤,标签覆盖了很多数据.我想将所有标签在缩放图的右侧对齐,所有空白都在此处(例如,x轴上的120上方),然后它们可以用更长的线连接到端点.但是我还没有实现这一目标.我已经尝试按照此处,但没有得到我要寻找的距离或对齐方式.

任何指导将不胜感激.

解决方案

您正在geom_label_repel()中寻找xlim=ylim=参数.它们的工作方式与轴限制相同,但是它们是排斥"标签的限制.您可以玩一下,但这是将该行更改为以下内容的结果:

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

您的里程可能会有所不同,因为输出取决于图形设备的分辨率和宽高比.

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.

解决方案

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.

这篇关于对齐ggrepel标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆