5

我想创建一个云,它使用 ggiraph 包中的工具提示显示有关该点的信息。我可以创建一个只有一个信息(来自一列)的云,但我想添加来自三列的信息。下面我添加了一张我想要实现的图片和代码。代码是正确的,但图中只有一列的信息。

图片显示了我想要实现的目标

#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)
4

1 回答 1

5

您可以使用paste0以下所有值获取工具提示:

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

然后geom_point_interactive(tooltip=df$col_C)你可以使用geom_point_interactive(tooltip=df$tooltip)

希望能帮助到你!

于 2017-05-29T06:51:39.617 回答