R语言 柱状图 geom_col 与 geom_bar 与geom_histogram(直方图)

library(tidyverse)
#----案例1:绘制频数柱状图
data.frame(x  = c("A", "B", "C"),
           y = c(rep("negative", 7), rep("positive", 11))) %>% 
  ggplot(aes(x = x, fill = y)) + geom_bar() +
  
  # coord_flip() +   # 转为横向,从下到上
  
  # scale_fill_manual(    # 修改颜色
  #   values = c("white", "firebrick"),
  #   guide = FALSE) +    # 图例
  
  # scale_fill_manual(
  #   values = RColorBrewer::brewer.pal(n = 2, name = 'Blues')) +

  # theme(
  #   legend.key =    # legend加上边框线条
  #     element_rect(
  #       colour = "grey55")) +

  # theme_bw() +    # 外部主题
  # theme(panel.grid = element_blank()) +    # 内部主题
  
  # scale_y_continuous(
  #   labels = scales::percent)     # 坐标轴标签改为百分比
  
  # labs(x = "Groups",
  #      y = "Percentage",
  #      fill = "Disease") +

  annotate(
    "text", x = 1, y = 3, 
    label = "50%", 
    colour = "white", fontface = "bold") + # 注释
  
  annotate(
    "text", x = 2, y = 4, 
    label = "67%", 
    colour = "white", fontface = "bold") +
  
  annotate(
    "text", x = 3, y = 4, 
    label = "67%", 
    colour = "white", fontface = "bold")

  # https://zhuanlan.zhihu.com/p/443503128
  # https://blog.csdn.net/g_r_c/article/details/19673625

  # https://mp.weixin.qq.com/s/t6V09wXHeNdoXRR8BuSPiw
  # https://www.delftstack.com/zh/howto/r/scale_y_continuous-in-r/
  # https://thomasadventure.blog/zh/posts/ggplot2-percentage-scale/

  # t <- mydata %>% table(); t
  # tc <- t %>% cbind(row = t %>% rowSums()); tc
  # tr <- tc %>% rbind(col = tc %>% colSums()); tr
  # 
  # cbind(
  #   table(mydata),
  #   row = rowSums(
  #     table(mydata)))
  # 
  # rbind(
  #   cbind(
  #     table(mydata),
  #     row = rowSums(
  #       table(mydata))),
  #   col = colSums(
  #     cbind(
  #       table(mydata),
  #       row = rowSums(
  #         table(mydata)))))

#----案例2:总结
# geom_col 需要提供x(分类变量)和y(数值变量,映射在y轴)
# geom_bar 只需要提供x,自动统计频数、频率,映射在y轴
# position = identity重叠,dodge分散,stack按频数堆积(左上→右下),fill按频率堆积
# Excel堆积左下→右上
# https://www.jianshu.com/p/57ec06c83fba
# ggplot2高效实用指南 https://www.jianshu.com/p/2dc81b91131e
# 透视、逆透视

# geom_col(分类变量,数值变量) = qplot(geom = "col")
data.frame(x = c("A", "B", "C"),
           y = 1:18) %>% 
  ggplot(mapping = aes(x = x, y = y, fill = y)) + geom_col()

# geom_bar(分类变量) = stat_count
data.frame(x  = c("A", "B", "C"),
           y = c(rep("negative", 7), rep("positive", 11))) %>% 
  ggplot(aes(x = x, fill = y)) + geom_bar()

# geom_bar(分类变量,stat = "bin") = geom_histogram = stat_bin ≈ geom_freqpoly
ggplot(diamonds, aes(price, fill = cut)) +
  geom_bar(stat = "bin")
ggplot(diamonds, aes(price, colour = cut)) +
  geom_freqpoly()



案例1

geom_col 需要提供x(分类变量)和y(数值变量,映射在y轴)

geom_bar 只需要提供x,自动统计频数、频率,映射在y轴

position = identity重叠,dodge分散,stack按频数堆积(左上→右下),fill按频率堆积

案例2
ggplot2一页多图排版的简便方法:gridExtra::grid.arrange()
以及https://www.jianshu.com/p/c154ca35530b

  • 7
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值