使用日期时的 ggplotly 和 geom_bar - 最新版本的 plotly (4.7.0) [英] ggplotly and geom_bar when using dates - latest version of plotly (4.7.0)
问题描述
假设你有以下df:
x <- as.Date(c("1963-06-01", "1964-06-01", "1965-06-01","1966-06-01"))
y <- c(1162.7, 975.4, 1280.3, 1380.0)
data<- data.frame(x, y)
当你使用 ggplot 绘制它时,一切似乎都正常:
when you plot it using ggplot, everything seems to work:
ggplot(data = data, aes(x = x, y = y)) +
geom_bar(stat = "identity")
但是,如果我们在它周围添加一个 ggplotly 环绕,图形就会消失.
However, if we add a ggplotly wrap around it, the graph disappears.
ggplotly(ggplot(data = data, aes(x = x, y = y)) +
geom_bar(stat = "identity"))
我收到一条警告消息:
我们建议您使用开发版的 ggplot2ggplotly()
.
We recommend that you use the dev version of ggplot2 with
ggplotly()
.
现在,如果我删除日期格式,gglotly 确实可以工作.
Now, if I remove the date format, the gglotly does work.
x <- c("1963-06-01", "1964-06-01", "1965-06-01","1966-06-01")
y <- c(1162.7, 975.4, 1280.3, 1380.0)
data<- data.frame(x, y)
ggplotly(ggplot(data=data) +
geom_bar(aes(x = x, y = y), stat = "identity"))
因此,ggplotly 处理带有日期的 geom_bar 似乎存在问题.有没有办法解决这个问题?
So, there seems to be an issue with ggplotly handling geom_bar with dates. Is there a way to solve this?
推荐答案
这似乎是 Mac 中的一个问题,似乎与 geom_bar 处理日期的方式有关.
This appears to be a problem in Mac and seems to be related with the way geom_bar handles dates.
我发现添加 as.POSIXct()
可以解决问题.
I found that adding as.POSIXct()
fixes the issue.
x <- c("1963-06-01", "1964-06-01", "1965-06-01","1966-06-01")
y <- c(1162.7, 975.4, 1280.3, 1380.0)
data<- data.frame(x, y)
ggplotly(ggplot(data=data) +
geom_bar(aes(x = as.POSIXct(x), y = y), stat = "identity"))
这篇关于使用日期时的 ggplotly 和 geom_bar - 最新版本的 plotly (4.7.0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!