R语言绘制分类变量柱状图

  • 背景
    在这里插入图片描述

Excel中无法实现分组区分颜色。
右边的图是我手动点击各个柱形修改分组颜色的。
请问如何在R和Excel中分别实现右图?

  • 解决:
# 数据准备 --------------------------------------------------------------------
library(tidyverse)
a <- readxl::read_excel("E:/histgram_filled_by_groups.xlsx", 
                       sheet = "Sheet2",
                       col_names = TRUE) # 如果保留行名,read.csv(row.names = 1); 代码(包括管道符号)在注释前面,否则无法运行注释后的换行内容
a <- a %>% tidyr::fill(type, .direction = "down")
a <- a %>% mutate(type = 
                    type %>% factor(levels = LETTERS[1:3]),
                  month = 
                    month %>% factor(levels = c("Jan","Feb","Mar")))
a <- a %>% arrange(type, month, value)
a %>% glimpse()

# 方法一:数据叠加 ------------------------------------------------------------------
a %>% ggplot(mapping = 
               aes(x = month,
                   y = value,
                   colour = type,
                   group = type)) + # 需提前设置分组
  geom_line() + geom_point() # 散点折线图

# 方法二:合并变量 -----------------------------------------------------------
a %>% mutate(num = 1:7) %>% # 用于给新变量排序
  unite(col = type_month,
        c(num, type, month),
        sep = "_",
        remove = FALSE) %>% # 合并为新变量
  ggplot(mapping = 
           aes(x = type_month,
               y = value,
               col = type,
               group = type)) + 
  geom_line() + geom_point()

# 方法三:柱状图 -----------------------------------------------------------
a %>% mutate(num = 
               seq(from = 1, by = 1,
                   length.out = a %>% nrow())) %>%
  unite(col = type_month,
        c(num, type, month),
        sep = "_",
        remove = FALSE) %>%
  ggplot(mapping = 
           aes(x = type_month,
               y = value,
               col = type,
               group = type,
               fill = type)) + 
  geom_col()

# 方法四:累积柱状图 ---------------------------------------------------------------
a %>% 
  ggplot(mapping = 
           aes(x = month,
               y = value,
               col = type,
               group = type,
               fill = type)) + 
  geom_col()

  • 结果展示
    在这里插入图片描述

  • 致谢

感谢上帝的恩典!

  真正的圣洁要求我们有两重的动机:
  透过对祂的恩典表示感恩来荣耀祂;
  透过服侍有需要的人来造福人。
  圣洁也要求行事的态度与事情的实质相符:换句话说,我们应合宜地做每一件事。

——巴刻

  • 遇到的问题及解决

问题:横坐标只能画三组,无法组内进行对比
解决:
需要组合mutate、unite函数把两组因子变量融合成一组变量。然后再用ggplot+geom_col()画图就好啦

a %>% mutate(num = 
               seq(from = 1, by = 1,
                   length.out = a %>% nrow())) %>%
  unite(col = type_month,
        c(num, type, month),
        sep = "_",
        remove = FALSE)

问题:将字符向量转化为因子向量
解决:检查格式自定义factor函数的水平levels

问题:geom_histogram错误:stat_bin() can only have an x or y aesthetic.
解决:直方图(geom_histogram)与柱形图有区别。

柱状图有geom_bar() 和 geom_col():
geom_bar() 经过统计变换(count, …prop…);
geom_col()不经过统计变换,代表的就是该分类变量的实际值。
参考资料:https://www.jianshu.com/p/05391806ab80

问题:处理合并单元格和缺失值
解决:
处理合并单元格:
openxlsx::read.xlsx(fillMergedCells = TRUE) # 填充所拆散的合并单元格
处理拆散单元格(向下填充缺失值):
tidyr::fill(type, .direction = “down”) # 向下填充

  • 其他
    请问有人知道如何在Excel中自动化实现吗?

Guess you like

Origin blog.csdn.net/weixin_42683052/article/details/121559337