可以通过以下步骤实现:
install.packages("ggplot2")
library(ggplot2)
df <- data.frame(
Category = c("A", "B", "C"),
Variable1 = c(10, 15, 20),
Variable2 = c(5, 8, 12),
Variable3 = c(7, 9, 11)
)
ggplot(df, aes(x = Category)) +
geom_bar(aes(y = Variable1, fill = "Variable1"), stat = "identity", position = "dodge") +
geom_bar(aes(y = Variable2, fill = "Variable2"), stat = "identity", position = "dodge") +
geom_bar(aes(y = Variable3, fill = "Variable3"), stat = "identity", position = "dodge") +
labs(title = "Multiple Variable Bar Chart", x = "Category", y = "Value") +
scale_fill_manual(values = c("Variable1" = "red", "Variable2" = "blue", "Variable3" = "green")) +
theme_minimal()
在上述代码中,我们使用aes函数指定x轴的变量为Category,y轴的变量分别为Variable1、Variable2和Variable3。fill参数用于指定不同变量的颜色。labs函数用于设置图表的标题和坐标轴标签。scale_fill_manual函数用于手动设置不同变量的颜色。theme_minimal函数用于设置图表的主题样式。
这是一个使用ggplot创建多个变量的组合条形图的示例。根据具体的数据和需求,可以进行相应的调整和定制。
领取专属 10元无门槛券
手把手带您无忧上云