从 ggplot 对象到 plotly 的转换失败

Failed conversion from ggplot object to plotly

我刚刚建造了一个新的玻璃容器,我正在尝试随着时间的推移监测湿度水平,以确保它已设置为最佳植物生长,我想我会使用 R 来做到这一点。我正在使用 POSIXct 记录 date/time 湿度测量值,然后将其记录到一个 ggplot 中,然后我尝试将其变成一个 plotly 对象;不幸的是,这最后一步不起作用。我不确定是什么原因造成的,因为我对 plotly 的经验很少。这是我的代码;如果它 messy/not 达标,我深表歉意,我 a) 累了 b) 一名学生,所以也欢迎任何提示!

注释掉情节线时,我得到了这个(这就是我想要的):

如果我可以 'anchor' 从某个时间起每 6 小时的日期标签,我也很喜欢 - 使用 scale_x_datetime limits/breaks 取决于测量的次数,我'我想每天在 00:00、06:00、12:00 和 18:00 休息一下。我该怎么做?

library(ggplot2)
library(lubridate)
library(scales)
library(gridExtra)
library(plotly)
library(hrbrthemes)

measurement.time = as.POSIXct(c("2020-08-23 21:45 GMT", "2020-08-23 22:45 GMT"), tz = 'Europe/London', format = "%Y-%m-%d %H:%M")

humidity = c(99,95)

data.forplot = data.frame(measurement.time, humidity)

Sys.setenv(TZ='Europe/London')

viv.plot = ggplot(data.forplot, aes(x = measurement.time, y = humidity)) +
  geom_point() +
  geom_line(alpha = 0.3) +
  ylab("Humidity (%)") +
  scale_y_continuous(limit=c(0,100),oob=squish) +
  scale_x_datetime(name = "Date", date_labels = "%B %d %H:%M") +
  ggtitle(~""*underline(Vivarium~Humidity~Levels)) +
  theme(plot.title = element_text(hjust=0.5, size =18 )) +
  geom_area(fill="#69b3a2", alpha=0.5)

viv.plot = ggplotly(viv.plot)

print(viv.plot)

以下代码片段适用于 plotly,不幸的是标题没有下划线。

measurement.time = as.POSIXct(c("2020-08-23 21:45 GMT", "2020-08-23 22:45 GMT"), tz = 'Europe/London', format = "%Y-%m-%d %H:%M")
humidity = c(99,95)
data.forplot = data.frame(measurement.time, humidity)

viv.plot = ggplot(data.forplot, aes(x = measurement.time, y = humidity)) +
   geom_point() +
   geom_line(alpha = 0.3) +
   ylab("Humidity") +
   scale_y_continuous(limit=c(0,100),oob=squish) +
   scale_x_datetime(name = "Date", date_labels = "%B %d %H:%M") +
   ggtitle("Vivarium Humidity Levels") +                            #Works
#   ggtitle(expression(underline("Vivarium Humidity Levels"))) +    #does not work with plotly, works with ggplot
   theme(plot.title = element_text(hjust=0.5, size =18, face="bold" )) +
   geom_area(fill="#69b3a2", alpha=0.5)

print(ggplotly(viv.plot))

Plotly 似乎不理解“表达式”语法,因此导致了问题。希望这对您的项目来说足够好。