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

Rem*_*i.b 14 graphics plot r graph 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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

use*_*650 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))
}
Run Code Online (Sandbox Code Playgroud)

然后情节

 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()
Run Code Online (Sandbox Code Playgroud)


eip*_*i10 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)) 
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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


mps*_*amm 5

更新的答案

ggplot23.3.0 修复了这个问题。当使用和args 时,使用该geom_pointrange()函数将在图例中呈现水平误差线:xminxmax

library(ggplot2)
library(dplyr)

df <- mtcars |> 
  mutate(gear = as.factor(gear),
         am = as.factor(am)) |> 
  group_by(gear, am) |> 
  summarise(cyl_mean = mean(cyl),
            cyl_upr =  mean(cyl) + sd(cyl)/sqrt(length(cyl)),
            cyl_lwr = mean(cyl) - sd(cyl)/sqrt(length(cyl)))

ggplot(df, 
       aes(x=cyl_mean, y=gear, 
           color = am, group = am)) +
  geom_pointrange(aes(xmin = cyl_lwr,
                      xmax = cyl_upr),
                  position = position_dodge(0.25))
Run Code Online (Sandbox Code Playgroud)

创建于 2023-05-16,使用reprex v2.0.2

旧答案

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))
Run Code Online (Sandbox Code Playgroud)

误差线

或作为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))
Run Code Online (Sandbox Code Playgroud)