r - 堆积面积图中不显示彩色堆积区域
问题描述
我有下面的数据框,我正在尝试使用ggplot()
and创建堆积面积图ggplotly()
。但是,虽然图表似乎没有错误地生成,但我无法显示彩色区域。
x_dt<-structure(list(interval_end = c("2021-08-14", "2021-08-14", "2021-08-14",
"2021-08-14", "2021-08-15", "2021-08-15", "2021-08-15", "2021-08-15",
"2021-08-16", "2021-08-16", "2021-08-16", "2021-08-16", "2021-08-17",
"2021-08-17", "2021-08-17", "2021-08-17", "2021-08-18", "2021-08-18",
"2021-08-18", "2021-08-18", "2021-08-19", "2021-08-19", "2021-08-19",
"2021-08-19", "2021-08-20", "2021-08-20", "2021-08-20", "2021-08-20",
"2021-08-21", "2021-08-21", "2021-08-21", "2021-08-21", "2021-08-22",
"2021-08-22", "2021-08-22", "2021-08-22", "2021-08-23", "2021-08-23",
"2021-08-23", "2021-08-23", "2021-08-24", "2021-08-24", "2021-08-24",
"2021-08-24", "2021-08-25", "2021-08-25", "2021-08-25", "2021-08-25",
"2021-08-26", "2021-08-26", "2021-08-26", "2021-08-26", "2021-08-27",
"2021-08-27", "2021-08-27", "2021-08-27", "2021-08-28", "2021-08-28",
"2021-08-28", "2021-08-28", "2021-08-29", "2021-08-29", "2021-08-29",
"2021-08-29", "2021-08-30", "2021-08-30", "2021-08-30", "2021-08-30",
"2021-08-15", "2021-08-15", "2021-08-15", "2021-08-15", "2021-08-16",
"2021-08-16", "2021-08-16", "2021-08-16", "2021-08-17", "2021-08-17",
"2021-08-17", "2021-08-17", "2021-08-18", "2021-08-18", "2021-08-18",
"2021-08-18", "2021-08-19", "2021-08-19", "2021-08-19", "2021-08-19",
"2021-08-20", "2021-08-20", "2021-08-20", "2021-08-20", "2021-08-21",
"2021-08-21", "2021-08-21", "2021-08-21", "2021-08-22", "2021-08-22",
"2021-08-22", "2021-08-22"), inv_bin = c("(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]"), N = c(58L, 55L, 38L, 25L, 0L,
9L, 13L, 6L, 2L, 11L, 13L, 5L, 44L, 120L, 69L, 48L, 17L, 103L,
66L, 61L, 20L, 69L, 67L, 63L, 29L, 63L, 69L, 51L, 28L, 41L, 29L,
36L, 2L, 8L, 19L, 10L, 2L, 17L, 16L, 9L, 22L, 65L, 52L, 50L,
34L, 45L, 62L, 68L, 49L, 39L, 54L, 51L, 89L, 70L, 42L, 51L, 59L,
105L, 59L, 65L, 3L, 24L, 23L, 16L, 45L, 36L, 18L, 13L, 58L, 64L,
51L, 31L, 2L, 20L, 26L, 11L, 46L, 131L, 82L, 53L, 61L, 223L,
135L, 109L, 37L, 172L, 133L, 124L, 49L, 132L, 136L, 114L, 57L,
104L, 98L, 87L, 30L, 49L, 48L, 46L)), row.names = c(NA, -100L
), class = c("data.table", "data.frame"))
# Packages
library(ggplot2)
library(dplyr)
library(viridis)
library(hrbrthemes)
library(plotly)
# Plot
p<-ggplot(x_dt, aes(x=interval_end, y=N, fill=inv_bin)) +
geom_area(alpha=0.6 , size=.5, colour="white") +
scale_fill_viridis(discrete = T) +
theme_ipsum() +
ggtitle("Chart 1")
ggplotly(p)
预期结果如下:
解决方案
需要一些数据准备:
- 从字符到日期格式的日期
N
将具有相同值的日期和 inv_bin 的计数结合起来。
# Packages
library(ggplot2)
library(dplyr)
library(viridis)
library(hrbrthemes)
library(forcats)
library(plotly)
x_dt1 <-
x_dt %>%
mutate(interval_end = as.Date(interval_end, "%Y-%m-%d"),
inv_bin = factor(inv_bin, levels = c("(0.0, 10.0]", "(10.0, 70.0]", "(70.0, 330.0]", "(330.0, inf]"), ordered = TRUE)) %>%
group_by(interval_end, inv_bin) %>%
summarise(N = sum(N))
p <-
ggplot(x_dt1, aes(x = interval_end, y = N, fill = fct_rev(inv_bin)))+
geom_area(alpha = 0.6 , size = 0.5, colour = "white", stat = "identity", orientation = "x") +
scale_fill_viridis(discrete = TRUE) +
labs(fill = NULL)+
theme_ipsum() +
ggtitle("Chart 1")+
theme(legend.position = "bottom")
p
# not printed
# ggplotly(p)
由reprex 包创建于 2021-09-01 (v2.0.0 )
推荐阅读
- javascript - 如何在chart.js中制作没有ticks.min/max的列的固定宽度?
- java - 在java中将替代节点从一个单链表复制到另一个链表
- c# - Azure DevOps 评论中的 Ping (@) 用户
- matplotlib - 如何控制 matplotlib 中的词干标记大小?
- pandas - 使用 seaborn 或 matplotlib 分组箱线图的数据格式
- python - 在python中将不同工作表中的列合并到单个工作表中
- mysql - 从 sample.sql 文件导入的数据的日志文件
- ios - 来自 Firebase 的通知停止出现在 iOS 上
- postgresql - 从文件更改行顺序的 Postgresql 复制
- angular - Typescript - 覆盖库原型函数