时间序列在plot_ly中表现得很奇怪

问题描述 投票:0回答:1

数据:

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

enter image description here

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

ggplotly(plot)

enter image description here

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

r ggplot2 time-series plotly
1个回答
0
投票

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

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

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.