旋转ggplot图例中的垂直线 [英] rotate vertical line in ggplot legend
问题描述
我希望将图的图例从垂直线旋转到水平线,但要使实际线在图中保持垂直.有许多黑客类型
是否有使用 ggstance
的建议?谢谢
编辑
由于 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_glyph
s. 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屋!