Polygon disappears when Plotly labels are defined - R

Viewed 46

I am trying to plot a polygon hull using ggplot and plotly.

While without label polygons are shown in the plot, when I add extra labels in aesthetics the polygons disappear.

library(data.table)
library(ggplot2)
library(dplyr)
library(plotly)

df <- data.table(continent = c(rep("America",3), rep("Europe",4)),
                     state = c("USA", "Brasil", "Chile", "Italy", "Swiss", "Spain", "Greece"),
                     X = rnorm(7, 5, 1),
                     Y = rnorm(7, -13, 1)
    )
    
    df$X_sd = sd(df$X)
    df$Y_sd = sd(df$Y)
    
    
    hull2 <- df %>%
      group_by(continent) %>%
      slice(chull(X,Y))
    
    

p <- df %>%
  ggplot( aes(x=X,
              y=Y, 
              fill = continent,
              color = continent,
              label=state))+
  geom_polygon(data = hull2,
               lwd = 1,
               alpha = 0.1,
               linetype = "dashed")+
  geom_errorbarh(aes(xmin = X - X_sd,
                     xmax = X + X_sd),
                 size = 0.5,
                 alpha = 0.3) +
  geom_errorbar(aes(ymin = Y - Y_sd,
                    ymax = Y + Y_sd),
                size = 0.5,
                alpha = 0.3) +
  geom_point(shape=21,
             color="black",
             size=3)+
  theme_bw()+
  theme(legend.position = "none")

ggplotly(p)
1 Answers

How odd! If you most label = state to the aes for the last geom_ you'll get the standard warning, but it works and the state shows up in the tooltip.

The designation of color = continent shows up, as well. I am going to guess that you're not interested in having that in your tooltip, so I've added how you could change that at the end. There is a tooltip with the continent listed two times, but with the information about how to remove the color, you'll see how you might make further adjustments depending on the trace.

p <- df %>%
  ggplot(aes(x = X, y = Y, 
             fill = continent,
             color = continent #,
             # label = state)
         )) +
  geom_polygon(data = hull2, lwd = 1,
               alpha = 0.1, linetype = "dashed") +
  geom_errorbarh(aes(xmin = X - X_sd,
                     xmax = X + X_sd),
                 size = 0.5, alpha = 0.3) +
  geom_errorbar(aes(ymin = Y - Y_sd,
                    ymax = Y + Y_sd),
                size = 0.5, alpha = 0.3) +
  geom_point(shape = 21, 
             color = "black",
             size = 3, aes(label = state)) +
  theme_bw() + theme(legend.position = "none")
p

ggplotly(p)

enter image description here

To remove the color from the tooltip, assign ggplotly to an object. Then you can remove the string from the 7th and 8th trace.

p1 = ggplotly(p)

lapply(7:8,
       function(i){
         p1$x$data[[i]]$text <<- stringr::str_replace(p1$x$data[[i]]$text, 
                                                      "continent: black<br />", 
                                                      "")
       })
p1

enter image description here

FYI, there are 8 traces that make up your plot. The first trace has the double continent text.

Related