How do I add additional information to the cloude tooltip in ggiraph packages in R?

I would like to create a cloud that shows point information using a tooltip in ggiraph packages. I can create a cloud with only one information (from one column), but I would like to add information from three columns. Below I've added the picture I want to achieve and some code. The code is correct, but the graph contains information from only one column.

The image shows what I want to achieve

#lib.
library(ggiraph)
library(ggplot2)
library(shiny)

#create data frame
col_A=c(123,523,134,543,154)
col_B=c(100,200,300,400,500)
col_C=as.character(c("food_1", "food_2", "food_3", "food_4", "food_5"))
df=data.frame(col_A, col_B, col_C)
df$col_C <- as.character(df$col_C)


#ui.
ui <- fluidPage(    
  ggiraph::ggiraphOutput("plot1"))



#server
server <- function(input, output) {

  gg <- ggplot(data = df ,aes(x = col_A, y = col_B)) + 
geom_point_interactive(tooltip=df$col_C)
  # I would like to plot like this: geom_point_interactive(tooltip=c(df$col_A, df$col_B, df$col_C))
  # but i causes error: Aesthetics must be either length 1 or the same as the data (5): tooltip

  output$plot1 <- renderggiraph({ 
    ggiraph(code= print(gg))})
}

shinyApp(ui = ui, server = server)

      

+3


source to share


1 answer


You can use paste0

to get tooltip with all values ​​like this:

df$tooltip <- c(paste0("Name = ", df$col_C, "\n Column A = ", df$col_A, "\n Column B = ", df$col_B))

      



Then instead geom_point_interactive(tooltip=df$col_C)

you can usegeom_point_interactive(tooltip=df$tooltip)

Hope it helps!

+2


source







All Articles