r - 无法通过使用 ggplot2 从 excel 表调用来获取变量的绘图和日期日期
问题描述
我正在尝试为两个变量绘制 geom_col,我是从 excel 表中调用的。数据采用时间序列格式。
这是我的数据集。我想在一个栏中为所有日期绘制变量“Johor”和“TCK”。
head(df2)
# A tibble: 6 x 33
date Johor TCJ Kedah TCKe Kelantan TCKlntn Melaka TCMk N.Sembilan TCN9 Pahang
<date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 2020-01-25 1 1 0 0 0 0 0 0 0 0 0
2 2020-01-26 0 1 0 0 0 0 0 0 0 0 0
3 2020-01-27 0 1 0 0 0 0 0 0 0 0 0
4 2020-01-28 1 2 1 1 0 0 0 0 0 0 0
5 2020-01-29 0 2 0 1 0 0 0 0 0 0 0
6 2020-01-30 1 3 0 1 0 0 0 0 0 0 0
这是我的 ggplot2 代码
ggplot(df2,aes(x=date, y=Johor, col=c(TCJ+Johor), group=c(TCJ+Johor)))+
geom_col(aes(fill=c(TCJ+Johor)),width=0.5
)+
theme(axis.text.x = element_text(angle=15, vjust=0.4)) +
labs(title="",
subtitle="")
我也想在 x 实验室有所有日期,但对我来说,我知道我只能有月份
所需的绘图输出。
解决方案
关于堆积条形图:
看起来您需要将数据集以长格式放置(然后是 TCJ 和柔佛的过滤器):
library("tidyverse")
df2 %>%
pivot_longer(-date, names_to = "name", values_to = "value") %>%
filter(name %in% c("Johor", "TCJ")) %>%
ggplot(aes(date, value, fill = name)) +
geom_col(position = "stack")
推荐阅读
- javascript - 以角度反应形式绑定表单数组值
- javascript - 我的 Node 和 express 应用程序在错误处理中间件时没有收到 req.session?
- javascript - 如何在 React 中实现文本转语音
- phpunit - Laravel 测试 - 该名称已被使用
- python-3.x - 解析数据框列中的 json
- javascript - React Native Chart Kit - 条形图颜色从上到下淡出每个条形图
- python - 如何在 Python 中使用 Matplotlib 绘制时间线图?
- python - Python github 使用用户名和密码拉取
- android - Google Play 控制台读取的 .Keystore 值与 Keytool 不同
- vue.js - NuxtJS - 我想知道如何为特定页面实现加载