r - 基于R中的列绘制堆积条
问题描述
假设我有以下数据框:
x = c(10,11,12,13)
x1 = c(0.50,0.55,0.58,0.62)
df <- data.frame(debt= c(0,1,2,3), x = x, x1 = x1, x2 = x-x1)
我如何绘制如下图?
解决方案
像这样的例子:
library(ggplot2)
df2 <- reshape2::melt(df, id.vars = c("debt","x"))
ggplot(df2, aes(x = debt)) +
geom_col(aes(y = value, fill = factor(variable, levels = c("x2","x1"))), width = 0.5) +
geom_line(aes(y = x, colour = "steelblue"), size = 3) +
scale_fill_manual(values = c("x1" = "darkorange", "x2" = "grey50"), name = "") +
scale_colour_identity(guide = "legend", name = "", labels = "x") +
scale_y_continuous(name = "x", expand = c(0,0,0.2,0)) +
theme_minimal()
推荐阅读
- postgresql - 如何获取表返回函数的返回类型的列定义列表
- asp.net-core - .Net 5:在 Windows Server 2019 的 IIS 10 中托管 ASP.NET Core gRPC 服务
- swift - CollectionView + 页面控件
- go - 构建多维数组响应
- node.js - 是否可以在递归 MongoDB 结构上执行投影运算符?
- python - 在 Pine Script 策略中输入点差成本
- javascript - 如何自动播放以及从最后一个到第一个 - ScrollView
- python - pandas ValueError: Cannot setitem on a Categorical with a new category,先设置类别
- python - 来自 Docker 的文档:找不到满足要求的版本 apturl==0.5.2 (& others)
- excel - 打开excel文件时复制和重命名工作表