简体   繁体   English

如何在R中的ggiraph包中的工具提示cloude中添加更多信息?

[英]How add more information in tooltip cloude in ggiraph packages in R?

I would like to create a cloud which shows information about the point using tooltip in ggiraph packages. 我想创建一个云,它使用ggiraph包中的工具提示显示有关该点的信息。 I can create a cloud with only one information (from one column), but I would like to add information from three columns. 我可以创建一个只包含一个信息的云(来自一列),但我想从三列添加信息。 Below I added a picture what I want to achieve and code. 下面我添加了一张我想要实现的图片和代码。 The code is correct but on the plot is information from only one column. 代码是正确的,但在图上只有一列的信息。

Picture 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)

You can use paste0 to get the tooltip with all the values as follows: 您可以使用paste0获取具有所有值的工具提示,如下所示:

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

Then instead of geom_point_interactive(tooltip=df$col_C) you can use geom_point_interactive(tooltip=df$tooltip) 然后你可以使用geom_point_interactive(tooltip=df$tooltip)代替geom_point_interactive(tooltip=df$col_C) geom_point_interactive(tooltip=df$tooltip)

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM