如何从R中的ggplotly()图形而不是数值转换的图形中可视化实际日期?

时间:2018-05-16 21:21:25

标签: plotly r-plotly ggplotly

当我将光标放在数据点上时,我想使用ggplotly图形来显示实际日期格式而不是数字(默认情况下已转换)。提前谢谢!

这是一个简单的例子:

 require(plotly)
 require(ggplot2)
    x <- c("01/01/2007","04/03/2008","28/11/2008","13/06/2009")
    y <- c(25, 50, 75, 100)
    x_lab <- "date"
    y_lab <- "score"
    (mydata <- as.data.frame(cbind(x,y)))
    mydata$x <- as.Date(mydata$x, "%d/%m/%Y")
    ggplot(mydata, aes(x=x, y=y)) +
      geom_point()
   ggplotly()

Click here to see the plot

1 个答案:

答案 0 :(得分:0)

使用plot_ly()函数代替ggplotly()解决问题!!

以下是具有相同数据的代码:

require(ggplot)
require(plotly)
x <- c("01/01/2007","04/03/2008","28/11/2008","13/06/2009")
y <- c(25, 50, 75, 100)
x_lab <- "date"
y_lab <- "score"
mydata <- as.data.frame(cbind(x,y))
mydata$x <- as.Date(mydata$x, "%d/%m/%Y")
str(mydata)
plot_ly(mydata, x = ~x, y = ~y, type = 'scatter', mode = 'markers',
        marker = list(size = 10))

Click here too see the new plot

或者,它仍然可以使用ggplotly完成!

require(ggplot)
require(plotly)
x <- c("01/01/2007","04/03/2008","28/11/2008","13/06/2009")
y <- c(25, 50, 75, 100)
x_lab <- "date"
y_lab <- "score"
mydata <- as.data.frame(cbind(x,y))
mydata$x <- as.Date(mydata$x, "%d/%m/%Y")
myplot <- ggplot(mydata, aes(x=x, y=y, label1=x, label2=y)) +
  geom_point()
ggplotly(myplot, tooltip = c("label1", "label2"))

New plot using ggplotly function!