加载gcookbook包,探索cabbage_exp维度和结构

  1. 加载gcookbook包,探索cabbage_exp维度和结构
  2. 以Date为横坐标,Weight为纵坐标,Cultivar为分组变量,作簇状柱状图
  3. 在第二题的基础上,以“Pastel2”为填充色,作柱状图
  4. 在第三题的基础上,柱状图上标注Weight具体数字
  5. 在第二题的基础上,作堆叠柱状图,Y轴无需统一为100%
  6. 在第二题的基础上,纵坐标统一为100%,作堆叠柱状图
# 1
library(gcookbook)
dim(cabbage_exp)  
str(cabbage_exp)  

# 2
library(ggplot2)

ggplot(cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(title = "簇状柱状图", x = "日期", y = "重量") +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 15)  
  )

# 3
ggplot(cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar)) +
  geom_bar(stat = "identity", position = "dodge") +
  scale_fill_brewer(palette = "Pastel2") +
  labs(title = "簇状柱状图", x = "日期", y = "重量") +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 15)  
  )

# 4
ggplot(cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar)) +
  geom_bar(stat = "identity", position = "dodge") +
  scale_fill_brewer(palette = "Pastel2") +
  geom_text(aes(label = Weight), position = position_dodge(width = 0.9), vjust = -0.5) +
  labs(title = "柱状图", x = "日期", y = "重量") +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 15)  
  )

# 5
ggplot(cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar)) +
  geom_bar(stat = "identity", position = "stack") +
  labs(title = "堆叠柱状图", x = "日期", y = "重量") +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 15) 
  )

# 6
ggplot(cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar)) +
  geom_bar(stat = "identity", position = "fill") +
  scale_y_continuous(labels = scales::percent) +
  labs(title = "堆叠柱状图", x = "日期", y = "比例") +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 15) 
  )
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值