I have the following example dataframe that I am trying to create a plotly with, it was working until I defined the shapes to be different
df <- tibble::tibble(
Year = c("2018", "2018", "2018", "2018", "2018", "2018", "2018", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2020", "2020", "2020", "2020", "2020", "2020", "2020", "2021", "2021", "2021", "2021", "2021", "2021", "2021", "2022", "2022", "2022", "2022", "2022", "2022", "2022", "2023", "2023", "2023", "2023", "2023", "2023", "2023", "2024", "2024", "2024", "2024", "2024", "2024", "2024", "2025", "2025", "2025", "2025", "2025", "2025", "2025", "All Years", "All Years", "All Years", "All Years", "All Years", "All Years", "All Years"),
Perc = c(69, 97, 43, 94, 79, 92, 11, 59, 97, 37, 95, 77, 93, 32, 60, 97, 33, 94, 77, 92, 75, 60, 97, 44, 93, 76, 92, 96, 67, 97, 57, 94, 79, 93, NaN, 67, 97, 99, 95, 80, 93, NaN, 86, 98, NaN, 95, 84, 94, NaN, NaN, 99, NaN, 96, NaN, 98, NaN, 100, 100, 97, 96, 88, 87, 77),
plotshape = as.character(c(19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 2, 19, 19, 19, 19, 19, 19, 2, 19, 19, 2, 19, 19, 19, 19, 2, 19, 2, 19, 19, 19, 19, 2, 19, 19, 19, 2, 19, 19, 19, 2, 19, 2, 19, 2, 19, 19, 19, 19, 19, 19, 19, 19)),
Check = c("E", "A", "F", "B", "D", "C", "G", "E", "A", "F", "B", "D", "C", "G", "E", "A", "F", "B", "D", "C", "G", "E", "A", "F", "B", "D", "C", "G", "E", "A", "F", "B", "D", "C", "G", "E", "A", "F", "B", "D", "C", "G", "E", "A", "F", "B", "D", "C", "G", "E", "A", "F", "B", "D", "C", "G", "A", "B", "C", "D", "E", "F", "G"))
The ggplot is working correctly but when I use ggplotly the lines between different shapes are no longer connected (the shapes are based on certain conditions in the real data to highlight the point to the end user to make it more obvious.
shp <- c("19"=19,"2"=2)
ggplot(df, aes(Check, Perc, col=Year, group=Year, shape=plotshape)) +
geom_point() +
geom_line() +
theme(panel.background = element_blank(),
axis.line = element_line(colour="black"),
legend.position = "top") +
guides(shape = "none") +
scale_shape_manual(values=shp)
ggplotly(p=ggplot2::last_plot()) %>%
config(displayModeBar = FALSE) %>%
layout(legend = list(title=list(text="Year"),orientation = "h", x=0, y=-0.15))
How to join the lines but keep the shapes different for certain years and checks?
I think this gets you what you need. The key was to move the mapping of shape=plotshape
to the geom_point() call, rather than having it in ggplot().
df <- tibble::tibble(
Year = c("2018", "2018", "2018", "2018", "2018", "2018", "2018", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2020", "2020", "2020", "2020", "2020", "2020", "2020", "2021", "2021", "2021", "2021", "2021", "2021", "2021", "2022", "2022", "2022", "2022", "2022", "2022", "2022", "2023", "2023", "2023", "2023", "2023", "2023", "2023", "2024", "2024", "2024", "2024", "2024", "2024", "2024", "2025", "2025", "2025", "2025", "2025", "2025", "2025", "All Years", "All Years", "All Years", "All Years", "All Years", "All Years", "All Years"),
Perc = c(69, 97, 43, 94, 79, 92, 11, 59, 97, 37, 95, 77, 93, 32, 60, 97, 33, 94, 77, 92, 75, 60, 97, 44, 93, 76, 92, 96, 67, 97, 57, 94, 79, 93, NaN, 67, 97, 99, 95, 80, 93, NaN, 86, 98, NaN, 95, 84, 94, NaN, NaN, 99, NaN, 96, NaN, 98, NaN, 100, 100, 97, 96, 88, 87, 77),
plotshape = as.character(c(19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 2, 19, 19, 19, 19, 19, 19, 2, 19, 19, 2, 19, 19, 19, 19, 2, 19, 2, 19, 19, 19, 19, 2, 19, 19, 19, 2, 19, 19, 19, 2, 19, 2, 19, 2, 19, 19, 19, 19, 19, 19, 19, 19)),
Check = c("E", "A", "F", "B", "D", "C", "G", "E", "A", "F", "B", "D", "C", "G", "E", "A", "F", "B", "D", "C", "G", "E", "A", "F", "B", "D", "C", "G", "E", "A", "F", "B", "D", "C", "G", "E", "A", "F", "B", "D", "C", "G", "E", "A", "F", "B", "D", "C", "G", "E", "A", "F", "B", "D", "C", "G", "A", "B", "C", "D", "E", "F", "G"))
library(ggplot2)
library(plotly)
shp <- c("19"=19,"2"=2)
ggplot(df, aes(Check, Perc, col=Year, group=Year)) + #,
geom_line() +
geom_point(aes(shape=plotshape)) +
theme(panel.background = element_blank(),
axis.line = element_line(colour="black"),
legend.position = "top") +
guides(shape = "none") +
scale_shape_manual(values=shp)
ggplotly(p=ggplot2::last_plot()) |>
config(displayModeBar = FALSE) |>
layout(legend = list(title=list(text="Year"),orientation = "h", x=0, y=-0.15))