数据为 POSIXct 时,ggplotly() 不显示 geom_vline/geom_hline [英] ggplotly() does not display geom_vline / geom_hline when data is POSIXct

查看:24
本文介绍了数据为 POSIXct 时,ggplotly() 不显示 geom_vline/geom_hline的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用时间标记"制作图表.这些时间标记是某些日期的垂直线.时间数据为 POSIXct 格式.我想使用 Plotly 很棒的交互界面并在其中使用我的 ggplot 对象.

问题是这些时间标记"在使用 ggplotly() 后没有显示出来.我已经尝试过 plotly::add_segments() 但它不起作用.这里有两个可复制的例子:

1.对于非 POSIXct 数据,它可以正常工作

<块引用>

# 虚拟数据集df2 = data.frame(id = 1:10, measure = runif(10, 0, 20))events2 = data.frame(number = c(2,3,8))# ggplot 图p2 = ggplot() + geom_line(data = df2, aes(x = id, y = measure)) +geom_vline(data = events2, aes(xintercept = events2$number), color = "red")p2# 正确显示 geom_vline 的绘图图ggplotly(p2)

<强>2.使用 POSIXct 数据不显示正确的结果

<块引用>

# 虚拟数据集df = data.frame(date = seq(as.POSIXct("2017-07-01", tz = "UTC", format = "%Y-%m-%d"),as.POSIXct("2018-04-15", tz = "UTC", format = "%Y-%m-%d"),1个月"),测量 = runif(10, 0, 20))events = data.frame(date_envents = as.POSIXct(c("2017-10-12", "2017-11-12", "2018-03-15"), tz = "UTC", format = "%Y-%m-%d"))# ggplot 图p = ggplot() + geom_line(data = df, aes(x = date, y = measure)) +geom_vline(数据=事件,aes(xintercept=事件$日期),颜色=红色")p# 无法正确显示 geom_vline 的绘图图ggplotly(p)

我见过一些解决方法(比如这个:

I am trying to make a graph with "time markers". These time markers are vertical lines for certain dates. Time data are POSIXct format. I would like to use the awesome interactive interface of Plotly and use my ggplot objects in it.

The problem is that these "time markers" doesn't show in after using ggplotly(). I ave already tried with plotly::add_segments() but it does not work. Here are two reproductible examples :

1. With non-POSIXct data it works fine

# dummy dataset
df2 = data.frame(id = 1:10, measure = runif(10, 0, 20))
events2 = data.frame(number = c(2,3,8))
# ggplot graph
p2 = ggplot() + geom_line(data = df2, aes(x = id, y = measure))  +
  geom_vline(data = events2, aes(xintercept = events2$number), color = "red")
p2
# plotly graph that displays the geom_vline properly
ggplotly(p2)

2. With POSIXct data is doesn't display the correct result

# dummy dataset
df = data.frame(date = seq(as.POSIXct("2017-07-01", tz = "UTC", format = "%Y-%m-%d"),
                           as.POSIXct("2018-04-15", tz = "UTC", format = "%Y-%m-%d"),
                           "1 month"),
                measure = runif(10, 0, 20))
events = data.frame(date_envents = as.POSIXct(c("2017-10-12", "2017-11-12", "2018-03-15"), tz = "UTC", format = "%Y-%m-%d"))
# ggplot graph
p = ggplot() + geom_line(data = df, aes(x = date, y = measure))  +
  geom_vline(data = events, aes(xintercept = events$date), color = "red")
p
# plotly graph that does not display the geom_vline properly
ggplotly(p)

I have seen some workaround (like this one : Add vertical line to ggplotly plot) but it is "complicated". Is there a more simple way to solve this problem ?

I am using Windows 10 with R version 3.5.0, RStudio and the following packages : library(tidyverse) and library(plotly)

解决方案

A simple workaround is to set the xintecept of the geom_vline to numeric.

sample data

df = data.frame(date = seq(as.POSIXct("2017-07-01", tz = "UTC", format = "%Y-%m-%d"),
                           as.POSIXct("2018-04-15", tz = "UTC", format = "%Y-%m-%d"),
                           "1 month"),
                measure = runif(10, 0, 20))
events = data.frame(date_envents = as.POSIXct(c("2017-10-12", "2017-11-12", "2018-03-15"), tz = "UTC", format = "%Y-%m-%d"))

code

p = ggplot() + geom_line(data = df, aes(x = date, y = measure))  +
  geom_vline(data = events, aes(xintercept = as.numeric(events$date)), color = "red")

result

ggplotly(p)

这篇关于数据为 POSIXct 时,ggplotly() 不显示 geom_vline/geom_hline的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆