r - 如何将日期放在 barplot() x Axis 上?

标签 r date bar-chart axis

>dates
"2020-10-03" "2020-10-04" "2020-10-21"
>class(dates)
[1] "Date"

values= c(110,90,90)
cols=ifelse(values>=0,"green","red")
barplot(height = values, width = dates,col = cols, xlab = "values") # error here

Ops.Date(space,mean(width)) 中的错误:* 未为“Date”对象定义

最佳答案

更简单的选择是公式方法

barplot(values ~ dates, col = cols, xlab = "values")

-输出

enter image description here


根据?barplot,'height'、'width'都是向量Date是一个带有一些属性的向量。因此,如果我们使用 as.numeric 转换为 vector,它将起作用

barplot(height = values, width = as.numeric(dates),col = cols, xlab = "values")

数据

dates <- structure(c(18538, 18539, 18556), class = "Date")

关于r - 如何将日期放在 barplot() x Axis 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64489144/

相关文章:

r - 如何在 R 中制作时间序列箱线图

r - 如何解释该分类 TreeMap 中的预测?

php - MySQL - DATE_SUB() 和 DATE_SUB() 错误之间的搜索查询

r - 如何在 kable/formattable 表中打印 unicode 符号?

r - 如何在 R 中运行 SQL 左连接?

sql - 如何从 PostgreSQL 中的另一个表生成日期范围 + 计算更早的日期?

java - Java 中的日期格式化程序

r - 突出显示facet_wrap ggplot中没有数据的位置

javascript - 条形图的 Chart new.js 自定义问题

javascript - 如何在 d3 中的条形图更新数据之间转换?