I\'m having problems stopping the y-axis text from overlapping with the ticks using ggplotly around ggplot. How can I fix this? I\'ve tried the fol
I found both answers above quite useful. However, I noticed that the layout_ggplotly() function given above works correctly only when x and y axis titles exist. If either of those is missing -- for example, as a result of theme(axis.title.x = element_blank()) -- then positional reference using ...[[1]]... and ...[[2]]... refers to wrong titles / annotations.
I wrote a function that I used in my project to address such limitation, I believe this builds up on the previous answers.
annotatify <- function(gg, x.y = -0.05, y.x = -0.05) {
wip <- gg[['x']][['layout']][['annotations']] %>%
enframe() %>%
mutate(value = map(value, as_tibble)) %>%
unnest(cols = c(value)) %>%
filter(!is.na(annotationType)) %>%
mutate(
x = case_when(x == 0.5 ~ x, TRUE ~ x.y),
y = case_when(y == 0.5 ~ y, TRUE ~ y.x)
) %>%
select(name, text, x, y) %>%
unique()
if (nrow(wip) == 2) {
for (i in 1:nrow(wip)) {
if (wip$x[i] == 0.50) {
gg[['x']][['layout']][['annotations']][[1]][['y']] <- wip$y[i]
} else {
gg[['x']][['layout']][['annotations']][[2]][['x']] <- wip$x[i]
}
}
} else if (wip$y == 0.5) {
gg[['x']][['layout']][['annotations']][[1]][['x']] <- wip$x
} else {
gg[['x']][['layout']][['annotations']][[1]][['y']] <- wip$y
}
gg
}