【问题标题】:Displaying the labels in a box at the top right-hand corner of the figure在图形右上角的框中显示标签
【发布时间】:2020-10-20 00:38:44
【问题描述】:

如下图所示,州名有时离线很远。要考虑的一种选择不是将标签放在线条旁边,而是将它们包含在图形右上角的框中。我该怎么做?

这是我认为需要修改的代码部分。

    geom_line(aes(x =Year, y = Age.Adjusted.Rate, colour = State), data = d_filtered_top5_fe)+
  
  ggrepel::geom_text_repel(aes(x =Year, y = Age.Adjusted.Rate, colour = State, label = State, fontface = 'bold'), data = d_filtered_top5_fe %>% 
                             
                             filter(Year == max(Year)),
                           segment.color = 'transparent',
                           size = 2.5
  ) +

【问题讨论】:

  • 您能否通过编辑您的问题并将dput() 的输出粘贴到其上,使用dput(d_filtered_top5_fe) 添加您的数据样本!

标签: r ggplot2 geom-text


【解决方案1】:

没有您的数据,很难制定出准确的解决方案。我想可能有几种方法可以解决这个问题。最简单的一种是使用图例。我们可以尝试这样的事情:

library(ggplot2)
ggplot(data = mtcars)+
geom_line(aes(x =wt, y = mpg, colour = factor(cyl))) + 
  theme( legend.position = c(0.9, .9),
  ) +
  facet_wrap(vars(am))

如果你能提供一个可重复的例子,它会帮助其他人帮助你。

【讨论】:

  • 谢谢。如何为 0 和 1 地块设置两个单独的图例?另外,我想知道我是否可以根据它们的值对图例标签进行排序。似乎 R 按字母顺序对它们进行了排序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-05
  • 2023-02-23
  • 1970-01-01
  • 2018-03-01
  • 2022-01-16
  • 2021-12-03
相关资源
最近更新 更多