[英]How to get mouse over labels in a shiny ggplot2 polar plot?
我正在用鼠標懸停在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網站上
這是最終的Shiny代碼。 我並沒有使用很多閃亮的東西,所以可以改善它,但是它對我有用。
# 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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.