代码之家  ›  专栏  ›  技术社区  ›  Remi.b

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

  •  20
  • Remi.b  · 技术社区  · 9 年前

    例如,考虑使用数据绘制的图 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 回复  |  直到 9 年前
        1
  •  12
  •   user20650    9 年前

    调整图例键

    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()
    
        2
  •  7
  •   eipi10    9 年前

    我不会给出一个在正常ggplot2工作流中工作的答案,所以现在,这里有一个简单的答案。关掉 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 比我强。

        3
  •  5
  •   mpschramm    5 年前

    这个 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
  •  3
  •   Sandy Muspratt    9 年前

    跟进@eipi10的使用建议 grid 用于编辑Grob的函数-相关Grob是段。有两种可能性:1)旋转分段格罗布;或2)编辑线段GROB端点的x和y坐标。

    library(ggplot2)
    library(Hmisc)
    
    library(grid)
    
    p = 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()
    
    g = ggplotGrob(p)
    
    # Get names of segment grobs
    grid.ls(grid.force(g))$name   # "GRID.segments"
    
    # Check the structure of the segment grobs
    str(getGrob(grid.force(g), gPath("GRID.segments"), grep = TRUE, global = TRUE))
    
    # Edit the segment grobs using the editGrob() function
    # 1) Rotate the segments
        g <- editGrob(grid.force(g), gPath("GRID.segments"), grep = TRUE, global = TRUE,
            vp = viewport(angle = 90)) 
    
    # 2) set end points of segments
    #    g <- editGrob(grid.force(g), gPath("GRID.segments"), grep = TRUE, global = TRUE,  
    #         x0 = unit(0.1, "npc"), y0 = unit(0.5, "npc"), x1 = unit(0.9, "npc"), y1 = unit(0.5, "npc"))
    
    # Draw it
    grid.newpage()
    grid.draw(g)
    
        5
  •  1
  •   Samuel Saari    4 年前

    编辑自: 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

    推荐文章