r - 应用 ggplotly() 后折线图未连接
问题描述
我有以下数据集
bike2 <- structure(list(dteday = c("2011-01", "2011-02", "2011-03", "2011-04",
"2011-05", "2011-06", "2011-07", "2011-08", "2011-09", "2011-10",
"2011-11", "2011-12", "2012-01", "2012-02", "2012-03", "2012-04",
"2012-05", "2012-06", "2012-07", "2012-08", "2012-09", "2012-10",
"2012-11", "2012-12"), cnt = c(38189L, 48215L, 63422L, 94870L,
135821L, 143512L, 141341L, 136691L, 127418L, 123511L, 102167L,
87323L, 96744L, 103137L, 164875L, 174224L, 195865L, 202830L,
203607L, 214503L, 218573L, 198841L, 152664L, 123713L)), row.names = c(NA,
-24L), class = c("tbl_df", "tbl", "data.frame"))
这是我目前拥有的图表
a = ggplot(transform(bike2, year = substr(dteday, 1, 4)),
aes(x=dteday, y=cnt, group = 1, color = year))+
geom_line(size = 1.2) +
labs(title = "Bike Rentals Per Month",
x = "Month/Year",
y = "Count") +
ggthemes::theme_solarized() +
theme(axis.text.x=element_text(angle=45, hjust=1))
a
但是,当我尝试将 ggplotly() 应用于图形时,该线断开连接。
plotly::ggplotly(a)
我该如何解决?
解决方案
一个相关的问题指出,情节描述的系列与 ggplot 略有不同,导致一些翻译问题,如这个。
一种解决方法是明确说明您要为每个元素显示的内容,使用geom_segment
:
library(dplyr)
bike2 %>%
mutate(dteday_next = lead(dteday),
year = substr(dteday, 1, 4),
cnt_next = lead(cnt)) %>%
ggplot(aes(x=dteday, xend = dteday_next,
y=cnt, yend = cnt_next, color = year))+
geom_segment() +
labs(title = "Bike Rentals Per Month",
x = "Month/Year",
y = "Count") +
ggthemes::theme_solarized() +
theme(axis.text.x=element_text(angle=45, hjust=1)) -> a
a
plotly::ggplotly(a)
推荐阅读
- reactjs - 显式类型检查后打字稿不可分配错误
- rust - 如何将生命周期参数添加到闭包而不返回引用
- jquery - 如何纠正字体调整按钮对齐
- reactjs - 我们如何动态加载由 webpack 配置的 entry 属性生成的 webpack 模块/包
- python - 使用 Python 将大型 CSV 文件导入 MySQL
- c# - 如何在 C# Selenium 中点击订阅 YouTube 按钮
- c++ - 我输入的任何数组大小超过 36603 时都会返回“堆栈溢出”错误。如何使字符串能够捕获整个 .txt 文件?
- java - SynchronousQueue 中的 NullPointerException
- docker - 无法重新启动 docker 容器,日志不包含任何消息
- asp.net-identity - 如何将 IdentityServer3 与 asp.net 身份数据库一起使用