首页 > 解决方案 > 时间序列在 plot_ly 中表现异常

问题描述

数据:

library(tidyverse)
library(lubridate)
library(plotly)

test <- tribble(
  ~Name, ~Date, ~Score, ~comp,
  "Ken" , "3-2-2020" , 5, "PC1",
    "Ken" , "3-2-2020" , 3, "PC1",
    "Ken" , "3-2-2020" , 4, "PC1",
    "Ken" , "3-2-2020" , 5, "PC1",
    "Ken" , "3-3-2020" , 5, "PC1",
    "Ken" , "3-3-2020" , 2, "PC1",
    "Ken" , "3-4-2020" , 5, "PC1",
    "Ken" , "3-5-2020" , 5, "PC1",
    "Ken" , "3-6-2020" , 4, "PC1",
    "Ken" , "3-2-2020" , 5, "PC2",
    "Ken" , "3-2-2020" , 3, "PC2",
    "Ken" , "3-2-2020" , 4, "PC2",
    "Ken" , "3-2-2020" , 5, "PC2",
    "Ken" , "3-3-2020" , 5, "PC2",
    "Ken" , "3-3-2020" , 2, "PC2",
    "Ken" , "3-4-2020" , 5, "PC2",
    "Ken" , "3-5-2020" , 5, "PC2",
    "Ken" , "3-6-2020" , 4, "PC2",
    "Ken" , "7-6-2020", 4, "PC3"
) %>%
  mutate(Date = lubridate::mdy(Date))

我想将其转换为多面时间序列图,如下所示:

plot <- test %>%
  ggplot(., aes(x=Date, y=Score)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  ylim(1,5.3) +
  theme_bw() +
  facet_wrap(. ~ comp)

plot

在此处输入图像描述

但是当我使用 plotly 时,它看起来像这样。

ggplotly(plot)

在此处输入图像描述

问题是什么?x轴看起来很奇怪

标签: rggplot2time-seriesplotly

解决方案


没关系。我只需要添加 scale_x_date(),如下所示:

ggplotly(plot + scale_x_date(date_labels = "%m-%Y"))

在此处输入图像描述


推荐阅读