【问题标题】:Showing all x axis label for discrete variable in ggplot bar plot在ggplot条形图中显示离散变量的所有x轴标签
【发布时间】:2020-10-28 20:31:13
【问题描述】:

我有一组数据,我想为这些数据绘制一个条形图。数据示例如下:

   yq        flag      n ratio
   <yearqtr> <fct> <int> <dbl>
 1 2011 Q1   0       269 0.610
 2 2011 Q1   1       172 0.390
 3 2011 Q2   0       266 0.687
 4 2011 Q2   1       121 0.313
 5 2011 Q3   0       239 0.646
 6 2011 Q3   1       131 0.354
 7 2011 Q4   0       153 0.668
 8 2011 Q4   1        76 0.332
 9 2012 Q1   0       260 0.665
10 2012 Q1   1       131 0.335
11 2012 Q2   0       284 0.676
12 2012 Q2   1       136 0.324
13 2012 Q3   0       197 0.699
14 2012 Q3   1        85 0.301
15 2012 Q4   0       130 0.688
16 2012 Q4   1        59 0.312
17 2013 Q1   0       273 0.705
18 2013 Q1   1       114 0.295
19 2013 Q2   0       333 0.729
20 2013 Q2   1       124 0.271

我想要做的基本上是绘制每个季度每个标志的比率。我写了下面的代码:

data$flag <- as.factor(data$flag)
ggplot(data=data, aes(x=yq, y=ratio, fill=flag)) +
  geom_bar(stat="identity", color="black", position=position_dodge())+
  theme_minimal() +
  scale_fill_manual(values=c('#999999','#E69F00'))

结果如下图:

但我真正想要的是在 x 轴上显示所有四分之一,并且可能以垂直形式显示更好的可视化。我尝试使用 scale_x_discrete(limits=yq) 但是,这不是正确的答案并返回错误。我该怎么做?

【问题讨论】:

  • 下次请将您的数据提供为dput(data),这样可以节省我们尝试重新创建数据的时间。
  • @zx8754 你是对的。我应该这样做的。感谢您的评论

标签: r ggplot2 bar-chart axis-labels zoo


【解决方案1】:

您的 yq 列似乎采用年-季格式(如 zoo 包中所定义)。对于 ggplot,最好将其转换为日期格式并像这样指定中断:

library(ggplot2)
library(zoo)

data$flag <- as.factor(data$flag)

ggplot(data=data, aes(as.Date(yq), ratio, fill = flag)) +
  geom_col(color="black", position = position_dodge()) +
  scale_x_date(date_breaks = "3 months", guide = guide_axis(n.dodge = 2)) +
  labs(x = "Year / Quarter") +
  scale_fill_manual(values=c('#999999','#E69F00')) +
  theme_minimal() 

或者你可以堆叠列:


ggplot(data=data, aes(as.Date(yq), ratio, fill = flag)) +
  geom_col(color="black", position = position_stack()) +
  scale_x_date(date_breaks = "3 months", guide = guide_axis(n.dodge = 2)) +
  labs(x = "Year / Quarter") +
  scale_fill_manual(values=c('#999999','#E69F00')) +
  theme_minimal() 

数据

data <- structure(list(yq = structure(c(2011, 2011, 2011.25, 2011.25, 
2011.5, 2011.5, 2011.75, 2011.75, 2012, 2012, 2012.25, 2012.25, 
2012.5, 2012.5, 2012.75, 2012.75, 2013, 2013, 2013.25, 2013.25
), class = "yearqtr"), flag = c(0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 
0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L), n = c(269L, 
172L, 266L, 121L, 239L, 131L, 153L, 76L, 260L, 131L, 284L, 136L, 
197L, 85L, 130L, 59L, 273L, 114L, 333L, 124L), ratio = c(0.61, 
0.39, 0.687, 0.313, 0.646, 0.354, 0.668, 0.332, 0.665, 0.335, 
0.676, 0.324, 0.699, 0.301, 0.688, 0.312, 0.705, 0.295, 0.729, 
0.271)), row.names = c(NA, -20L), class = c("tbl_df", "tbl", 
"data.frame"))

【讨论】:

    【解决方案2】:

    我猜你正在使用 zoo 包,然后使用自定义中断设置 scale_x_yearqtr

    ggplot(data = data, aes(x = yq, y = ratio, fill = flag)) +
      geom_bar(stat = "identity", color = "black", position = position_dodge())+
      scale_x_yearqtr(breaks = unique(data$yq)) +
      theme_minimal() +
      scale_fill_manual(values = c('#999999', '#E69F00'))
    

    然后,如果需要,只需翻转:

    ggplot(data = data, aes(x = yq, y = ratio, fill = flag)) +
      geom_bar(stat = "identity", color = "black", position = position_dodge())+
      scale_x_yearqtr(breaks = unique(data$yq)) +
      theme_minimal() +
      scale_fill_manual(values = c('#999999', '#E69F00')) +
      coord_flip()
    

    数据

    > dput(data)
    structure(list(yq = structure(c(2011, 2011, 2011.25, 2011.25, 
    2011.5, 2011.5, 2011.75, 2011.75, 2012, 2012, 2012.25, 2012.25, 
    2012.5, 2012.5, 2012.75, 2012.75, 2013, 2013, 2013.25, 2013.25
    ), class = "yearqtr"), flag = structure(c(1L, 2L, 1L, 2L, 1L, 
    2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("0", 
    "1"), class = "factor"), n = c(269L, 172L, 266L, 121L, 239L, 
    131L, 153L, 76L, 260L, 131L, 284L, 136L, 197L, 85L, 130L, 59L, 
    273L, 114L, 333L, 124L), ratio = c(0.61, 0.39, 0.687, 0.313, 
    0.646, 0.354, 0.668, 0.332, 0.665, 0.335, 0.676, 0.324, 0.699, 
    0.301, 0.688, 0.312, 0.705, 0.295, 0.729, 0.271)), row.names = c(NA, 
    -20L), class = "data.frame")
    

    【讨论】:

    • 没错!我使用zoo 包生成year/quarter。谢谢,这解决了这个问题。你能解释一下scale_x_yearqtr到底在做什么吗?我以前看到scale_x_discretescale_x_continous 这个功能对我来说是全新的
    • @Roozbeh_you ,scale_x_yearqtr 的手册非常清楚,它有助于绘制动物园对象:“使用 ggplot2 绘制动物园对象的便利函数”。我只是根据数据唯一的季度值设置自定义休息时间。
    【解决方案3】:

    不妨试试这个:

    library(tidyverse)
    #Code
    df %>% 
      mutate(yq=factor(yq,levels = rev(unique(yq)),ordered = T)) %>%
      ggplot(aes(x=yq, y=ratio, fill=factor(flag))) +
      geom_bar(stat="identity", color="black", position=position_dodge())+
      theme_minimal() +
      scale_x_discrete(breaks=unique(df$yq))+
      scale_fill_manual(values=c('#999999','#E69F00'))+
      coord_flip()+labs(fill='flag')
    

    输出:

    或者这个:

    #Code 2
    df %>% 
      ggplot(aes(x=yq, y=ratio, fill=factor(flag))) +
      geom_bar(stat="identity", color="black", position=position_dodge())+
      theme_minimal() +
      scale_x_discrete(breaks=unique(df$yq))+
      scale_fill_manual(values=c('#999999','#E69F00'))+
      theme(axis.text.x = element_text(angle=90))+labs(fill='flag')
    

    输出:

    使用的一些数据:

    #Data
    df <- structure(list(yq = c("2011 Q1", "2011 Q1", "2011 Q2", "2011 Q2", 
    "2011 Q3", "2011 Q3", "2011 Q4", "2011 Q4", "2012 Q1", "2012 Q1", 
    "2012 Q2", "2012 Q2", "2012 Q3", "2012 Q3", "2012 Q4", "2012 Q4", 
    "2013 Q1", "2013 Q1", "2013 Q2", "2013 Q2"), flag = c(0, 1, 0, 
    1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1), n = c(269, 
    172, 266, 121, 239, 131, 153, 76, 260, 131, 284, 136, 197, 85, 
    130, 59, 273, 114, 333, 124), ratio = c(0.61, 0.39, 0.687, 0.313, 
    0.646, 0.354, 0.668, 0.332, 0.665, 0.335, 0.676, 0.324, 0.699, 
    0.301, 0.688, 0.312, 0.705, 0.295, 0.729, 0.271)), row.names = c(NA, 
    -20L), class = "data.frame")
    

    【讨论】:

    • 谢谢。我想第二张图就是我要找的;但是,当我尝试遵循您的代码时,我在 scale_x_discrete 中收到了一条错误消息,就像以前一样。错误是:Error: "mapped_discrete" objects can only be created from numeric vectors。那么我应该从yq 创建一个单独的 vecore 并将其传递给函数来解决它吗?
    • 谢谢,鸭子,我猜@zx8754 回答了我的问题。似乎错误消息是由于zoo 包结果的数据类型。我应该提供,所以你知道你正在处理什么类型的数据。吸取教训!
    猜你喜欢
    • 2020-10-24
    • 1970-01-01
    • 2013-06-21
    • 2019-01-28
    • 2016-05-14
    • 2021-12-03
    • 1970-01-01
    • 2021-11-01
    • 2014-02-12
    相关资源
    最近更新 更多