天天看点

《R数据可视化手册》——3.1 绘制简单条形图

本节书摘来异步社区《r数据可视化手册》一书中的第3章,第3.2节,作者:【美】winston chang,更多章节内容可以访问云栖社区“异步社区”公众号查看。

问题

如何绘制基于某个分类变量的簇状条形图?

方法

将分类变量映射到fill参数,并运行命令geom_bar(position="dodge")。

下面以cabbage_exp数据集为例演示一下绘图过程,cabbage_exp数据集包含两个分类变量cultivar和date及一个连续型变量weight。

ggplot(cabbage_exp, aes(x=date, y=weight, fill=cultivar)) +

  geom_bar(position="dodge",stat="identity")

  geom_bar(position="dodge", stat="identity", colour="black") +

  scale_fill_brewer(palette="pastel1")

ce <- cabbage_exp[1:5,] #复制删除了最后一行的数据集

ce

cultivar date weight

   c39 d16 3.18

   c39 d20 2.80

   c39 d21 2.74

   c52 d16 2.26

    c52 d20 3.11

  

ggplot(ce, aes(x=date, y=weight, fill=cultivar)) +

  geom_bar(position="dodge",stat="identity", colour="black") +

继续阅读