,可以通过以下步骤实现:
install.packages("ggplot2")
library(ggplot2)
# 创建示例数据
data <- data.frame(
Category = c("A", "B", "C", "D"),
Value1 = c(10, 15, 8, 12),
Value2 = c(5, 7, 9, 11)
)
plot <- ggplot(data, aes(x = Category))
plot <- plot + geom_bar(aes(y = Value1, fill = "Value1"), stat = "identity", position = "identity")
plot <- plot + geom_bar(aes(y = -Value2, fill = "Value2"), stat = "identity", position = "identity")
plot <- plot + coord_flip() # 翻转坐标轴
plot <- plot + scale_fill_manual(values = c("Value1" = "blue", "Value2" = "red")) # 设置填充颜色
plot <- plot + labs(x = "Category", y = "Value", fill = "") # 设置坐标轴和图例标签
print(plot)
背靠背条形图适用于比较两个相关变量在不同类别下的差异。它可以用于展示两组数据的对比情况,特别适用于呈现正负相关的数据。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云