旋转ggplot图例中的垂直线 [英] rotate vertical line in ggplot legend

查看:49
本文介绍了旋转ggplot图例中的垂直线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将图的图例从垂直线旋转到水平线,但要使实际线在图中保持垂直.有许多黑客类型

是否有使用 ggstance 的建议?谢谢

根据@Allan Cameron的评论

编辑

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

解决方案

您需要更改 key_glyph 参数,请参见可用的(v0.3.0)创建于2020-09-06 sup>

I'm looking to rotate the legend of a plot from vertical lines to horizontal but keep the actual lines vertical in the graph. There are a number of hack type solutions but I think this can now easily be done with ggstance package but I'm not sure how to achieve it.

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

Any suggestions using ggstance? thanks

EDIT per @Allan Cameron comment

As ggstance has been superseded by the latest version of ggplot, I'm happy to see any solution someone might have?

解决方案

You need to change the key_glyph argument, see the available key_glyphs. You need "path":

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

Created on 2020-09-06 by the reprex package (v0.3.0)

这篇关于旋转ggplot图例中的垂直线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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