【问题标题】:rotate vertical line in ggplot legend旋转ggplot图例中的垂直线
【发布时间】:2020-09-06 14:29:56
【问题描述】:

我希望将绘图的图例从垂直线旋转到水平线,但在图中保持实际线垂直。 solutions 有许多 hack 类型,但我认为现在可以使用 ggstance 包轻松完成,但我不确定如何实现。

library(tidyverse) 
library(ggstance)
df <- tibble(x = rnorm(40))
df_stats <-
  df %>%  summarise(
    mean = mean(x), 
    median = median(x)
  ) %>% 
  gather(key = legend, value = value, mean:median)

df %>% 
  ggplot(aes(x = x)) +
  geom_histogram(bins = 20) +
  geom_vline(data = df_stats, aes(xintercept = value, color = legend))

使用ggstance 有什么建议吗?谢谢

编辑@Allan Cameron 评论

由于 ggstance 已被最新版本的 ggplot 取代,我很高兴看到有人可能有任何解决方案?

【问题讨论】:

  • ggstance 已被最新版本的 ggplot 取代,正如 here 所解释的那样,所以我认为这不是正确的使用途径。
  • 感谢您的提示。看来key_glyph 下面的参数就是我所需要的!
  • 但是ggstance 是一个很好的指针,因为在它的文档中我发现了key_glyph 参数:)

标签: r ggplot2


【解决方案1】:

您需要更改key_glyph 参数,请参阅可用的key_glyphs。你需要“路径”:

library(tidyverse)
df <- tibble(x = rnorm(40))
df_stats <-
  df %>%  summarise(
    mean = mean(x), 
    median = median(x)
  ) %>% 
  gather(key = legend, value = value, mean:median)

df %>% 
  ggplot(aes(x = x)) +
  geom_histogram(bins = 20) +
  geom_vline(data = df_stats, aes(xintercept = value, color = legend),
             key_glyph = "path")

reprex package (v0.3.0) 于 2020 年 9 月 6 日创建

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    • 2020-08-09
    • 2012-04-04
    • 2019-05-05
    • 2022-01-16
    相关资源
    最近更新 更多