r - 如何将鼠标悬停在闪亮的 ggplot2 极坐标图中的标签上?
问题描述
我正在为我的 ggplot 2 极坐标图的标签而苦苦挣扎。
我的代码的简单版本(没有鼠标悬停在标签上):
library(dplyr)
library(shiny)
library(ggplot2)
# Define UI for application that plots features of iris
ui <- fluidPage(
br(),
# Sidebar layout
sidebarLayout(
# Inputs
sidebarPanel(
),
# Outputs
mainPanel(
plotOutput(outputId = "radarplot"),
br()
)
)
)
# Define server function required to create the radarplot
server <- function(input, output) {
# Create radarplot with iris dataset
output$radarplot <- renderPlot ({
iris %>%
ggplot(.) + geom_histogram(aes(y = Petal.Width, x = Species, fill = Species),
binwidth= 1,
stat= 'identity',
alpha = 1 ) +
geom_histogram(aes(y = Sepal.Width, x = Species, fill = Species),
binwidth= 1,
stat= 'identity',
alpha = 0.3) +
coord_polar()
})
}
# Create a Shiny app object
shinyApp(ui = ui, server = server)
我使用 plotly 制作了一个版本,试图在标签上添加鼠标。但是我没有得到雷达图。
library(dplyr)
library(shiny)
library(ggplot2)
library(plotly)
# Define UI for application that plots features of iris
ui <- fluidPage(
br(),
# Sidebar layout
sidebarLayout(
# Inputs
sidebarPanel(
),
# Outputs
mainPanel(
plotlyOutput(outputId = "radarplot"),
br()
)
)
)
# Define server function required to create the radarplot
server <- function(input, output) {
# Create radarplot with iris dataset
output$radarplot <- renderPlotly ({
iris %>%
ggplot(.) + geom_histogram(aes(y = Petal.Width, x = Species, fill = Species),
binwidth= 1,
stat= 'identity',
alpha = 1 ) +
geom_histogram(aes(y = Sepal.Width, x = Species, fill = Species),
binwidth= 1,
stat= 'identity',
alpha = 0.3) +
coord_polar()
})
}
# Create a Shiny app object
shinyApp(ui = ui, server = server)
理想情况下,当鼠标悬停在特定物种“翼”上时,我希望鼠标悬停在标签上以提供有关 Petal.Width、Sepal.Width 和 Species 的输出。
有什么建议如何让这些鼠标悬停在标签上?
解决方案
这是使用ggiraph
包的示例。首先需要创建工具提示。
library(tidyverse)
iris_group_means <-
iris %>%
group_by(Species) %>%
summarise_all(mean) %>%
mutate(tooltip = sprintf("Sepal Length: %1.2f\nSepal Width: %1.2f\nPetal Length: %1.2f\nPetal Width: %1.2f",
Sepal.Length, Sepal.Width, Petal.Length, Petal.Width)) %>%
select(Species, tooltip)
那么这个工具提示只需要作为一种美学来提供,而不是geom_histogram
使用该ggiraph::geom_histogram_interactive
功能。
my_gg <-
iris %>%
ggplot() +
geom_histogram(aes(y = Petal.Width, x = Species, fill = Species),
binwidth= 1,
stat= 'identity',
alpha = 1 ) +
ggiraph::geom_histogram_interactive(aes(y = Sepal.Width, x = Species, fill = Species, tooltip = tooltip),
binwidth= 1,
stat= 'identity',
alpha = 0.3) +
coord_polar()
ggiraph::ggiraph(code = print(my_gg))
然后可以在 Shiny 中使用它。涉及其他一些步骤,并且有一个单独的ggiraph::renderggiraph
功能可供使用。详细信息在ggiraph 网站上
这是最终的闪亮代码。我很少使用闪亮的东西,所以这可能会有所改进,但它对我有用。
# Define UI for application that plots features of iris
ui <- fluidPage(
br(),
# Sidebar layout
sidebarLayout(
# Inputs
sidebarPanel(
),
# Outputs
mainPanel(
ggiraph::ggiraphOutput(outputId = "radarplot"),
br()
)
)
)
# Define server function required to create the radarplot
server <- function(input, output) {
# Create radarplot with iris dataset
output$radarplot <- ggiraph::renderggiraph ({
iris_group_means <-
iris %>%
group_by(Species) %>%
summarise_all(mean) %>%
mutate(tooltip = sprintf("Sepal Length: %1.2f\nSepal Width: %1.2f\nPetal Length: %1.2f\nPetal Width: %1.2f",
Sepal.Length, Sepal.Width, Petal.Length, Petal.Width)) %>%
select(Species, tooltip)
iris <-
left_join(iris, iris_group_means, by="Species")
my_gg <-
iris %>%
ggplot() +
geom_histogram(aes(y = Petal.Width, x = Species, fill = Species),
binwidth= 1,
stat= 'identity',
alpha = 1 ) +
ggiraph::geom_histogram_interactive(aes(y = Sepal.Width, x = Species, fill = Species, tooltip = tooltip),
binwidth= 1,
stat= 'identity',
alpha = 0.3) +
coord_polar()
ggiraph::ggiraph(code = print(my_gg))
})
}
# Create a Shiny app object
shinyApp(ui = ui, server = server)
推荐阅读
- flutter - 无法在扩展中创建方法
- python - 我需要做一个计算项目价格而不将它们存储在数据库 django 中的函数
- typescript - '错误:找不到模块'来自 Typescript 中的相对路径
- java - 安排任务的 Cli java 应用程序,释放 cmd/终端并在预定时间执行任务
- excel - 在 Excel 中修改范围引用中间公式
- c# - 范围验证器显示错误消息但不阻止提交按钮单击
- php - 如何获取元属性="og:image"
- apache-kafka - 配置为需要 Kafka client.id
- firebase - StreamBuilder 中的数据在 Firebase RTDB get() 调用之外不断返回 null
- security - 无法理解 Web 应用程序的多种身份验证/技术方式