r - 如何使用日期数据创建线图?
问题描述
ggplot(data=A, aes(x = Date, y = WaterLevel)) + geom_line(aes(color = Location))
dput(droplevels(head(A, 20)))
structure(list(Date = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L), .Label = c("2018-01-01",
"2018-01-02", "2018-01-03", "2018-01-04"), class = "factor"),
Typhoon = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "", class = "factor"),
Location = structure(c(2L, 1L, 5L, 4L, 3L, 2L, 1L, 5L, 4L,
3L, 2L, 1L, 5L, 4L, 3L, 2L, 1L, 5L, 4L, 3L), .Label = c(" Upstream Af & Midstream Bf",
" Upstream Bf", "Downstream Af", "Downstream Bf", "Midstream Af"
), class = "factor"), WaterLevel = c(-0.02, -1.83, 0.56,
-3.64, 3.11, -0.01, -1.83, 0.56, -3.63, 3.11, 0.01, -1.83,
0.56, -3.64, 3.1, 0.01, -1.71, 0.83, -3.63, 3.1)), row.names = c(NA,
20L), class = "data.frame")
数据格式错了吗?
结果目前看起来像这样
解决方案
将“日期”列从类别转换factor
为有效Date
library(dplyr)
library(ggplot2)
A %>%
mutate(Date = as.Date(Date)) %>%
ggplot(aes(x = Date, y = WaterLevel)) +
geom_line(aes(color = Location))
推荐阅读
- reactjs - 在故事书 monorepo 中提供静态资产
- symfony - 尽管为实体正确设置了值,但为什么我会出错?
- powerbi - POWER BI - 如果计算的列被保存回模型并占用空间(在导入中),它们如何在直接查询模式下工作?
- vb.net - 我如何阅读 HasRow 然后更新?
- flutter - 从 Flutter 中的 Isolate 监听方法调用 UI 方法抛出异常
- https - 从 URL 中删除方案是好的做法吗?
- python - 在 tornado 的 ioloop 上使用 asyncio 方法(例如 create_future)
- flutter - Flutter ios release build 找不到快照
- javascript - 如何使用 Javascript API 获得道路元素的形状?
- rpm - RPM GPG key import from an RPM post install script