从 ggplot 创建的绘图中的轴位置

Axis position in plotly plot created from ggplot

ggplot 让我可以控制 x 轴标签的位置并在 x 轴上打断,但是当我将 ggplot 对象传递给 ggplotly 函数时,生成的 plotly 对象会丢失格式。

library(plotly)

df <- data.frame(
Date = seq(as.Date("2017-01-01"), as.Date("2020-01-01"), by = 30),
Value = rnorm(37)
)

p1 = ggplot(df) + geom_point(aes(x=Date, y = Value)) + 
scale_x_date(position = "top", date_breaks = "1 year", date_minor_breaks = 
"3 months")

ggplotly(p1)

使用上面提到的代码,x 轴值仍然绘制在 ggplotly 图中的底部,并且每 3 个月的断线也没有显示。

你可以试试这个:

f <- list(
side = "top"
)

ggplotly(p1) %>% layout(xaxis = f)