如何在ggplot2中旋转图例符号?

时间:2016-10-24 23:44:28

标签: r plot graph graphics ggplot2

例如,使用数据mtcars和函数coord_flip

来考虑此图
library(ggplot2)
library(Hmisc)

ggplot(mtcars,aes(x=gear,y=cyl)) + stat_summary(aes(color=as.factor(rep(1:2,16))),
fun.data=mean_cl_boot, position=position_dodge(0.4)) + coord_flip()

enter image description here

错误条在图表上是水平但在图例中垂直的事实困扰着我:)如何旋转这些符号?

5 个答案:

答案 0 :(得分:8)

调整图例键

GeomPointrange$draw_key <-  function (data, params, size)     {

         draw_key_vpath <- function (data, params, size) {
           # only need to change the x&y coords so that the line is horizontal
           # originally, the vertical line was `0.5, 0.1, 0.5, 0.9`
              segmentsGrob(0.1, 0.5, 0.9, 0.5, 
              gp = gpar(col = alpha(data$colour, data$alpha), 
              lwd = data$size * .pt, lty = data$linetype, 
              lineend = "butt"), arrow = params$arrow)
              }

    grobTree(draw_key_vpath(data, params, size), 
             draw_key_point(transform(data, size = data$size * 4), params))
}

然后绘制

 ggplot(mtcars,aes(x=gear,y=cyl)) + 
    stat_summary(aes(color=as.factor(rep(1:2,16))),
                  fun.data=mean_cl_boot, position=position_dodge(0.4)) + 
    coord_flip()

答案 1 :(得分:5)

我没有想出一个在正常的ggplot2工作流程中有效的答案,所以现在,这是一个hacky的答案。关闭stat_summary图例。然后,使用超出要绘制的实际数据范围的数据添加点和线主题。这将创建所需的点和水平线图例。然后将绘图轴限制设置为仅包括实际数据的范围,以便伪数据点不可见。

ggplot(mtcars, aes(x=gear, y=cyl, color=as.factor(rep(1:2,16)))) + 
  stat_summary(fun.data=mean_cl_boot, position=position_dodge(0.4), show.legend=FALSE) + 
  geom_line(aes(y=cyl-100)) +
  geom_point(aes(y=cyl-100), size=2.5) +
  coord_flip(ylim=range(mtcars$cyl)) 

enter image description here

另一种选择是使用网格函数将图例键的凹凸旋转90度,但我会留给那些比我更熟练grid的人。

答案 2 :(得分:3)

跟进@ eipi10建议使用$ node readlines.js this is the second line third [ 'this is', 'the second line', 'third' ] 函数来编辑grobs - 相关的grob是段。有两种可能性:1)旋转节段凹凸;或者2)编辑段凹凸的端点的x和y坐标。

grid

答案 3 :(得分:1)

ggstance软件包在此处提供了易于实施的解决方案:

library(ggplot2)
library(ggstance)

ggplot(mtcars,aes(x=cyl,y=gear)) + stat_summaryh(aes(color=as.factor(rep(1:2,16))),
                                                fun.data=mean_cl_boot_h, position = position_dodgev(height = 0.4))

errorbars

或作为geom

df <- data.frame(x = 1:3, y = 1:3)
ggplot(df, aes(x, y, colour = factor(x))) +
     geom_pointrangeh(aes(xmin = x - 1, xmax = x + 1))

答案 4 :(得分:0)

编辑自:https://gist.github.com/grantmcdermott/d86af2b8f21f4082595c0e717eea5a90

重点是使用 geom_pointrangeh 中的 ggstance 并记住指定 aes w.r.t. x 轴。

library(tidyverse)
library(broom)
library(hrbrthemes) 
library('ggstance')
library('jtools')

df = 
  mtcars %>%
  mutate(vs = factor(vs), am = factor(am))

fit1 = lm(mpg ~ vs * am * wt, data = df) 
fit1_coefs = tidy(fit1, conf.int = T) 

fit2 = lm(mpg ~ vs / am / wt, data = df)
fit2_coefs = tidy(fit2, conf.int = T) 


bind_rows(
  fit1_coefs %>% mutate(model = "Model 1"),
  fit2_coefs %>% mutate(model = "Model 2")
) %>%
  filter(grepl("wt", term)) %>%
  ## Optional regexp work to make plot look nicier  
  mutate(
    am = ifelse(grepl("am1", term), "Automatic", "Manual"),
    vs = ifelse(grepl("vs1", term), "V-shaped", "Straight"),
    x_lab = paste(am, vs, sep="\n")
  ) %>%
  ggplot(aes(col = model,y=x_lab, x=estimate, xmin=conf.low, xmax=conf.high)) +
  geom_pointrangeh(position = position_dodge(width = 0.5)) +
  guides(color = guide_legend(reverse = TRUE)) +
  geom_vline(xintercept = 0, col = "black",lty=4) +
    labs(x = NULL, y = NULL,title = "Title") +
  theme_nice() +
  theme(plot.title = element_text(hjust = 0.5))

enter image description here