r - R中带有正值和负值的堆积条形图
问题描述
我想在 R 中绘制一个堆积条形图,我的数据如下所示:
该表是针对日期的值,可以看出,存在具有不同侧面的重复日期。我想使用这些数据绘制条形图。
combined = rbind(x,y)
combined = combined[order(combined$Group.1),]
barplot(combined$x,main=paste("x vs y Breakdown",Sys.time()),names.arg = combined$Group.1,horiz = TRUE,las=2,xlim=c(-30,30),col = 'blue',beside = True)
想要一个堆积图,我可以在其中查看日期值。如何更改我的代码?
解决方案
您可以使用 轻松创建此图ggplot2
。这里有一段代码为您使用类似于您所拥有的数据框:
library(ggplot2)
my_data <- data.frame(
date = factor(c(1, 2, 2, 3, 3, 4, 5, 5, 6, 7, 8, 8)),
x = c(-2, 14, -8, -13, 3, -4, 9, 8, 3, -4, 8, -1)
)
g <- ggplot(my_data, aes(x = date, y = x)) +
geom_bar(
stat = "identity", position = position_stack(),
color = "white", fill = "lightblue"
) +
coord_flip()
这是输出:
显然,官方文档是开始更好地了解如何改进它的好方法。
推荐阅读
- sql - SAS SQL 中的枢轴翻译,更简单?
- c++ - 如何使用 SSE2 添加到变量?
- sql-server - 您可以在 GAC 以外的位置安装自定义 SSIS 组件吗?
- ios - 无法将 iOS 自定义框架添加到 KMM(Kotlin 多平台)模块(找不到 cinteropXXXIosArm64 FAILED 模块)
- multidimensional-array - 使用ndarray实现逐行操作
- google-analytics - 结合通用分析和 GA4 的脚本?
- javascript - Javascript 面试题:make [1,2,3].sum() run
- python-3.x - 如何从 aiogram python bot 运行任务?
- css - 输入类型编号,箭头下方为白色背景
- python - RuntimeError : 结果的 nnz 太大