Hello,
I am enjoying learning to get some easy interactivity out of plotly with ggplotly in R in RStudio.
I have what appears to be the common problem of positioning annotations on a date axis with a scatterplot,
despite using the exact same data that is correctly positioning the data points.
( such as with https://community.plotly.com/t/dates-on-x-axis-not-spaced-appropriately/767 ,
but in my case the dates are all at the left edge rather than distributed across the plot )
~
The values are R dates , explicitly coerced after loading the data. As mentioned, this is working fine for rendering the scatterplot dots.
I have already tried myself hardcoding a few string formats to see if any work and don’t see a change from being at the extreme left.
Can anyone tell me the correct type and format for x/y position for Annotations with Dates in R ?
(and ggplotly if that makes a difference)
reprex if anyone prefers it ~
suppressPackageStartupMessages(library(lubridate))
suppressPackageStartupMessages(library(ggplot2))
suppressPackageStartupMessages(library(plotly))
# ---- GGPlotDateHelpers ----
# from https://community.rstudio.com/t/scale-x-date-date-breaks-1st-and-15th-of-the-month/906
bimonthly <- function(x) {
x_range <- range(x, na.rm = TRUE)
date_range <- c(
floor_date(x_range[1], "month"),
ceiling_date(x_range[2], "month")
)
monthly <- seq(date_range[1], date_range[2], by = "1 month")
sort(c(monthly, monthly + days(14)))
}
# - - - Define the data as a hardcoded set for now - - -
demoDF <- data.frame(
stringsAsFactors = FALSE,
id = c(10948L,16822L,
13485L,14425L,17740L,10118L,14751L,18774L,
14687L,10630L,13427L,18987L,14899L,10596L,
12415L,11935L,14367L,19010L,12504L,11616L,18663L,
16361L,16020L,13642L,10793L),
date = c("9/29/2022",
"4/3/2023","4/3/2023","5/3/2023","11/8/2022",
"9/1/2022","11/17/2022","5/26/2022","4/12/2023",
"3/7/2023","2/21/2023","4/4/2023","3/7/2023",
"1/24/2023","4/25/2023","4/25/2023","5/10/2023",
"3/2/2023","5/26/2022","4/15/2022","9/22/2022",
"6/9/2022","4/27/2023","4/6/2022","8/9/2022"),
value = c(0L,0L,0L,5L,
0L,7L,0L,0L,13L,12L,0L,0L,3L,0L,0L,0L,0L,
0L,6L,16L,34L,0L,0L,0L,0L),
lat = c(-84.8,-84.8,
-83.8,-84.8,-84.8,-84.8,-84.8,-83.8,-84.8,
-84.8,-84.8,-84.8,-83.8,-84.8,-84.8,-84.8,-84.8,
-83.8,-84.8,-84.8,-84.8,-84.8,-83.8,-84.8,
-84.8),
long = c(40.4,38.4,38.4,
41.4,40.4,41.4,40.4,38.4,38.4,40.4,41.4,
39.4,41.4,39.4,38.4,39.4,39.4,38.4,41.4,
40.4,40.4,40.4,41.4,40.4,40.4)
)
# - - coerce the dates to Date - -
demoDF$date <- as.Date(demoDF$date, format = "%m/%d/%Y")
# - - - build the "manual" annotations (towards including clicktoshow) - - -
manualAnnotations <- list()
for (i in 1:NROW(demoDF)) {
tmp = list(
x = demoDF$date[[i]],
y = demoDF$value[[i]],
text = paste(
demoDF$id[[i]],
"<br><a href='https://google.com/maps/search/?api=1&query=", demoDF$long[[i]], ",", demoDF$lat[[i]], "'>location</a>",
"<br>date:", demoDF$date[[i]],
"<br>final data value:", demoDF$value[[i]],
sep=""
),
visible = TRUE, # < < < < < < < < < < < < < < <<<<<<<<<<< -----TO SHOW THE PROBLEM , make visible
clicktoshow = "onout"
)
manualAnnotations <- append(manualAnnotations, list(tmp))
}
# - - - declare the ggplot, specifying the data in this case - - -
myplot4 = ggplot(demoDF, aes(
x=date, y=value
)
)
#--- start the scatterplot ---
myplot4 = myplot4 + geom_point()
#--- define the axis ranges ---
# -- let's have the breaks as 1st and 15th, using helper function --
dateRangeEnds <- c(as.Date('2022-04-01'), as.Date('2023-06-30'))
breaksAsBimonthlyList <- bimonthly(dateRangeEnds)
myplot4 = myplot4 + scale_x_date(name="date of final value", breaks = breaksAsBimonthlyList, limits = c(as.Date('2022-04-01'), as.Date('2023-06-30')), expand=c(0,0))
myplot4 = myplot4 + scale_y_continuous(name="final value", breaks=seq(0,77,7), limits=c(0,85))
# #--- tweak the axis appearance ---
myplot4 = myplot4 + theme(axis.text.x = element_text(angle = 45, hjust=1))
# - - - Run it with ggplotly! - - -
ggplotly(myplot4, tooltip="", width = 1000) %>% layout(hovertext="", annotations=manualAnnotations)