代码之家  ›  专栏  ›  技术社区  ›  melbez

当x为离散变量时改变图y轴的下限

  •  0
  • melbez  · 技术社区  · 4 年前

    我过去对这类问题使用的一种解决方案是使用geom\u rect而不是geom\u bar。但是,这在这里不起作用,因为x轴是不连续的。

    我曾试图创建一个单独的、连续的x变量,但它确实弄乱了图形的格式。

    enter image description here

    代码:

    groups %>% 
      ungroup() %>% 
      mutate(message = fct_relevel(message, "Personal", "General"),
             enviroattitudeshalf = fct_relevel(enviroattitudeshalf, "Low Environmental Attitudes", "High Environmental Attitudes")) %>% 
      ggplot(aes(x = message, y = mean)) + 
      geom_col(width = 0.5, fill = "003900") +
      geom_text(aes(label = round(mean, digits = 1), vjust = -2)) + 
      geom_errorbar(aes(ymin = mean - se, ymax = mean + se), width = .2, position = position_dodge(.9)) + 
      labs(title = "Environment: Evaluations of Personal and General Convincingness",
           y = "Rating",
           x = "Personal evaluation or general evaluation") + 
      ylim(0, 8) +
      facet_wrap(~enviroattitudeshalf)
    

    structure(list(enviroattitudeshalf = c("Low Environmental Attitudes", 
    "Low Environmental Attitudes", "High Environmental Attitudes", 
    "High Environmental Attitudes"), message = c("General", "Personal", 
    "General", "Personal"), mean = c(3.89473684210526, 3.37894736842105, 
    4.43636363636364, 5.10909090909091), se = c(0.145460372156746, 
    0.19522803582675, 0.160549137262631, 0.171509247396541)), row.names = c(NA, 
    -4L), groups = structure(list(enviroattitudeshalf = c("High Environmental Attitudes", 
    "Low Environmental Attitudes"), .rows = structure(list(3:4, 1:2), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = 1:2, class = c("tbl_df", 
    "tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
    "tbl_df", "tbl", "data.frame"))
    
    1 回复  |  直到 4 年前
        1
  •  0
  •   Ronak Shah    4 年前

    使用 coord_cartesian :

    library(tidyverse)
    
    groups %>% 
      ungroup() %>% 
      mutate(message = fct_relevel(message, "Personal", "General"),
             enviroattitudeshalf = fct_relevel(enviroattitudeshalf, "Low Environmental Attitudes", "High Environmental Attitudes")) %>% 
      ggplot(aes(x = message, y = mean)) + 
      geom_col(width = 0.5, fill = "003900") +
      geom_text(aes(label = round(mean, digits = 1), vjust = -2)) + 
      geom_errorbar(aes(ymin = mean - se, ymax = mean + se), width = .2, position = position_dodge(.9)) + 
      labs(title = "Environment: Evaluations of Personal and General Convincingness",
           y = "Rating",
           x = "Personal evaluation or general evaluation") + 
      coord_cartesian(ylim = c(1, 8)) +  
      facet_wrap(~enviroattitudeshalf)
    

    enter image description here