Home > Software engineering >  ggplotly: dynamicTicks = T doesn't respect position attribute
ggplotly: dynamicTicks = T doesn't respect position attribute

Time:09-08

I have created a plot using ggplot which moves the positions of the points.

But when I use the attribute dynamicTicks = TRUE the positions of the point goes back to default.

Is there any way I can override this to keep the positions I have specified?

With dynamicTicks = FALSE

With dynamicTicks = TRUE

Edited to add reproducible code.

library(tidyverse)
library(plotly)

df <- structure(list(SerialNumber = c("634", "634", "567", "634", "634", 
                                      "562", "567", "567", "562", "562", "634", "569", "634", "634", 
                                      "634", "634", "634", "634", "634", "634", "634", "634", "561", 
                                      "634", "569", "569", "634", "634", "634", "567", "567", "567", 
                                      "634", "634", "567", "567", "567", "634", "562", "569"), `_device` = c("module1", 
                                                                                                             "module1", "module1", "module1", "module2", "module4", "module2", 
                                                                                                             "module2", "module4", "module1", "module1", "module4", "module1", 
                                                                                                             "module1", "module1", "module1", "module1", "module1", "module3", 
                                                                                                             "module1", "module1", "module3", "module2", "module1", "module1", 
                                                                                                             "module1", "module1", "module1", "module2", "module1", "module1", 
                                                                                                             "module3", "module1", "module1", "module1", "module1", "module1", 
                                                                                                             "module1", "module1", "module4"), `_source_program` = c("program1", 
                                                                                                                                                                     "program2", "program3", "program2", "program4", "program2", "program2", 
                                                                                                                                                                     "program1", "program2", "program2", "program2", "program2", "program2", 
                                                                                                                                                                     "program2", "program2", "program2", "program2", "program2", "ONprogram3", 
                                                                                                                                                                     "program1", "program3", "ONprogram3", "program6", "program1", 
                                                                                                                                                                     "program6", "program2", "program4", "program6", "program4", "program6", 
                                                                                                                                                                     "program6", "ONprogram3", "program6", "program2", "program4", 
                                                                                                                                                                     "program6", "program6", "program2", "program6", "program2"), 
                     date = structure(c(18891, 18906, 18937, 18940, 18943, 18949, 
                                        18952, 18956, 18966, 18972, 18983, 18981, 19007, 19023, 19036, 
                                        19036, 19037, 19066, 19072, 19082, 19082, 19088, 19086, 19091, 
                                        19106, 19106, 19115, 19109, 19122, 19142, 19145, 19151, 19149, 
                                        19149, 19157, 19157, 19169, 19175, 19185, 19192), class = "Date")), row.names = c(NA, 
                                                                                                                          -40L), spec = structure(list(cols = list(SerialNumber = structure(list(), class = c("collector_double", 
                                                                                                                                                                                                              "collector")), `_device` = structure(list(), class = c("collector_character", 
                                                                                                                                                                                                                                                                     "collector")), `_source_program` = structure(list(), class = c("collector_character", 
                                                                                                                                                                                                                                                                                                                                    "collector")), date = structure(list(format = ""), class = c("collector_date", 
                                                                                                                                                                                                                                                                                                                                                                                                 "collector"))), default = structure(list(), class = c("collector_guess", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                       "collector")), delim = ","), class = "col_spec"), class = c("spec_tbl_df", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   "tbl_df", "tbl", "data.frame"))

programs <- unique(df$`_source_program`)

nudgeAmounts <- ""

for (i in 1:length(programs)) {
  if (i == 1) {
    nudgeAmounts <- ifelse(df$`_source_program` == programs[i],as.double(paste0("0.", i)), 0)
  } else {
    nudgeAmounts <- ifelse(df$`_source_program` == programs[i],as.double(paste0("0.", i)), nudgeAmounts)
  }
  
}

shapes <- c(15,16,17,18,7,8)
names(shapes) <- programs

g <- ggplot(df, aes(date, SerialNumber, shape = `_source_program` , color =`_device`))  
  geom_point(position = position_nudge(y = nudgeAmounts), 
             size = 3
  )  
  theme_bw()   
  expand_limits(y = c(1, length(levels(as.factor(df$SerialNumber)))   1))   
  labs(y = "Serial Number", x = "Date")  
  scale_shape_manual(values = shapes)

g

ggplotly(g, dynamicTicks = TRUE) 

CodePudding user response:

The only way I could get this to work was to shove it down Plotly's throat. I tried a lot of different ways. I couldn't even get it to work correctly while using only Plotly.

I made two ggplotly objects: one with dynamic ticks and one without them. Essentially, we are going to keep the x-axis from one and the y-axis from the other.

plt <- ggplotly(g, dynamicTicks = T)
plt2 <- ggplotly(g)

# replace xaxis layout in plt2
plt2$x$layout$xaxis <- plt$x$layout$xaxis
invisible(lapply(1:length(plt2$x$data), # replace x-axis for each trace
                 function(k) {
                   plt2$x$data[[k]]$x <<- plt$x$data[[k]]$x
                 }))
plt2 # dynamic x-axis; jittered y-axis

enter image description here

And zoomed in, to show the dynamic x-axis:

enter image description here

  • Related