本文介绍了使用日期时,ggplotly和geom_bar-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")

ggplot有效

但是,如果我们在其周围添加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"))

ggplotly不起作用

我收到一条警告消息,内容为:

I get a warning message that says:

现在,如果我删除日期格式,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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 22:03