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