0

Is there a way, how to have by geom_line/geom_smooth instead of numeric date (see hovering info in the picture Index: 18824.96) date in a format like this "2022-02-24". See the reproducible code below:

library(tseries)
library(zoo)
# install.packages('quantmod')
library(quantmod)

# Ethereum
Ethereum <- get.hist.quote(instrument = "ETH-USD",
                          start = "2018-01-01",
                          end = Sys.Date(),
                          quote = "Close",
                          compression = "d")

# Dataframe
df <- cbind(Ethereum)
df <- fortify.zoo(df)

# Ethereum
library(ggplot2)
library(plotly)
g <- ggplot(data = df, aes(x=Index, y=Close)) +
  geom_line(col="cornflowerblue", lwd=0.5) +
  geom_smooth(col="red", lwd=0.5, se=FALSE, linetype = "dashed") +
  geom_vline(xintercept=as.numeric(as.Date("2022-02-24")), lwd=0.3, linetype=4) +
  xlab("Time") +
  ylab("Ethereum Price") +
  scale_x_date(date_labels = "%Y-%b", date_breaks = "4 months") +
  scale_y_continuous(breaks = seq(min(0), max(5000), by=500)) +
  theme_bw()

ggplotly(g)

enter image description here

Jaroslav Kotrba
  • 283
  • 1
  • 14
  • Could you put some example data for Ethereum -> `Error in new.session() : Could not establish session after 5 attempts.` – TarJae May 16 '22 at 19:48
  • Run this: install.packages('quantmod') - one need the latest version – Jaroslav Kotrba May 17 '22 at 06:53
  • @Jon Spring - I tried to use this answer, however, it does not solved my problem, so it is not a duplicate. I need to change Index format, not adding additional information. – Jaroslav Kotrba May 17 '22 at 06:54
  • It looks like the same problem to me, related to how plotly's interpolation for the smooth geom steps away from a date-formatted axis. It doesn't look like there's a way to change it, but you can specify custom text to get around that. Also here: https://stackoverflow.com/questions/63001515/ggplotly-is-breaking-my-geom-smooth-element and here: https://stackoverflow.com/questions/44770799/date-format-in-tooltip-of-ggplotly – Jon Spring May 17 '22 at 07:22

0 Answers0