user10624646
user10624646

Reputation:

How to make plotly x-axis position data on month only?

Currently have this data:

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

dates <- c("2019-03-01", "2019-04-01", "2019-05-01")
numbers <- c(50000, 60000, 70000)

data <- tibble(dates = as_datetime(dates), numbers = numbers)

plot_ly(data, x= ~dates, y=~numbers, showlegend = TRUE, marker = list(color = "blue"), type = "bar")

I would like the bars to be positioned over the month - and not straddle across different months - for example March 1st value is straddled across february too - is it not possible to have it just over March instead? the next bar over April etc!

Upvotes: 0

Views: 234

Answers (1)

Ali
Ali

Reputation: 1080

Use

dates <- c("2019-03-01", "2019-04-01", "2019-05-01")
numbers <- c(50000, 60000, 70000)

data <- tibble(dates,numbers)
data$dates <- factor(data$dates, levels = data[["dates"]])

plot_ly(data, x= ~dates, y=~numbers, showlegend = TRUE, marker = list(color = "blue"), type = "bar")

Instead of converting the dates into date-time format, you can extract the unique factors and plot along them.

Upvotes: 1

Related Questions