如何在 R 中的 ggiraph 包中的 tooltip cloude 中添加更多信息?
How add more information in tooltip cloude in ggiraph packages in R?
我想创建一个云,它使用 ggiraph 包中的工具提示显示有关该点的信息。我可以创建只有一个信息(来自一列)的云,但我想添加来自三列的信息。下面我添加了一张我想要实现的图片和代码。代码是正确的,但图上只有一列的信息。
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)
您可以使用 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$tooltip)
而不是 geom_point_interactive(tooltip=df$col_C)
希望对您有所帮助!
我想创建一个云,它使用 ggiraph 包中的工具提示显示有关该点的信息。我可以创建只有一个信息(来自一列)的云,但我想添加来自三列的信息。下面我添加了一张我想要实现的图片和代码。代码是正确的,但图上只有一列的信息。
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)
您可以使用 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$tooltip)
geom_point_interactive(tooltip=df$col_C)
希望对您有所帮助!