我想让我的Shiny图中的轴标签显示变量的标签(用户看到并选择的变量),而不是变量名本身。
因此,理想情况下,如果用户选择“犯罪份额”在y轴上绘制,则y轴标签将显示“犯罪份额”,而不是变量名“ CRIMESHARE”。我的图表现在执行后者。
我是Shiny的新手,我已经寻找了可以理解的解决方案,但是我很沮丧。我是否应该使用一系列if语句(即,如果用户选择了“ CRIMESHARE”->“ Crime share”)来编写反应式语句?如果是这样,关于如何最好地解决该问题的任何建议?还是有一种更简单,更闪亮的方式来解决此问题?我可以根据需要生成示例数据,但我认为这更多是一个概念性问题。
谢谢。
样本数据:
structure(list(ROADACCIDENT = c(142.69999694825, 120.40000152588,
130.19999694825, 155.10000610352, NA, 181.10000610352, 54.20000076294,
118.09999847413, 85.19999694825, 112.40000152588), CRIMESHARE = c(3142,
1695, 1554, 2806, 3368, 3164, 1359, 2013, 711, 2446), MURDER = c(267,
605, 309, 185, 65, 590, 93, 631, 501, 138), NAME = c("Khabarovsk Krai",
"Chelyabinsk Oblast", "Novosibirsk Oblast", "Amur Oblast", "Magadan
Oblast",
"Krasnoyarsk Krai", "Karachay-Cherkess Republic", "Moscow", "Moscow",
"Kostroma Oblast"), ID = c(29, 13, 49, 5, 41, 36, 26, 44, 44,
34), YEAR = c(2009, 1992, 1990, 2007, 2001, 2007, 1999, 2009,
1991, 2000)), row.names = c(608L, 255L, 1009L, 102L, 852L, 753L,
535L, 923L, 905L, 704L), class = "data.frame")
用户界面:
library(shiny)
library(tidyverse)
library(stringr)
library(rsconnect)
library(shinythemes)
library(plotly)
data(crime_master)
crime_plot <- crime_master
ui <- fluidPage(theme = shinytheme("cerulean"),
titlePanel("My title"),
sidebarLayout(
sidebarPanel(
h3("Select the inputs"),
selectInput(inputId = "y",
label = "Indicator to display on Y-axis",
choices = c("Road accidents" = "ROADACCIDENT",
"Crime share" = "CRIMESHARE",
"Murders" = "MURDER"),
selected = "ROADACCIDENT"),
selectizeInput(inputId = "region",
label = "Select regions",
choices = c(crime_plot$NAME),
multiple = TRUE,
options = list(maxItems = 5))),
mainPanel(
h3("Plot indicators over time"),
plotlyOutput(outputId = "scatterplot"))))
服务器:
server <- function(input, output){
regions_subset <- reactive({
req(input$region)
filter(crime_plot, NAME %in% input$region)})
output$scatterplot <- renderPlotly({
ggplotly(ggplot(data = regions_subset(),
aes_string(x = "YEAR", y = input$y, color = "NAME")) +
geom_point() +
labs(x = "Year", y = input$y) +
scale_color_discrete(name = "Regions"))})
output$event <- renderPrint({
k <- event_data("plotly_hover")
if (is.null(k)) "Hover on a point!" else d})}
shinyApp(ui = ui, server = server)
答案 0 :(得分:0)
当我删除req(inputs$region)
个反应式时,只要更改区域或犯罪类型,图形就会更改轴标签。您还可以在theme
中添加ggplotly
,以使单词在更改时不会与轴标签重叠:
server <- function(input, output){
regions_subset <- reactive({
filter(crime_plot, NAME %in% input$region)
})
output$scatterplot <- renderPlotly({
ggplotly(ggplot(data = regions_subset(),
aes_string(x = "YEAR", y = input$y, color = "NAME")) +
geom_point() +
labs(x = "Year", y = input$y) +
theme(text=element_text(size=10),
axis.text.y=element_text(angle=90, hjust=1)) +
scale_color_discrete(name = "Regions"))
})
output$event <- renderPrint({
k <- event_data("plotly_hover")
if (is.null(k)) "Hover on a point!" else d})}
shinyApp(ui = ui, server = server)
如果要使用标签而不是变量名,请仅更改以下两个块:
selectInput(inputId = "y",
label = "Indicator to display on Y-axis",
choices = c("Road accidents",
"Crime share",
"Murders"),
selected = "Road accidents"),
AND
output$scatterplot <- renderPlotly({
yvarnames <- c("ROADACCIDENT","CRIMESHARE","MURDER")
ggplotly(ggplot(data = regions_subset(),
aes_string(x = "YEAR", y = yvarnames[input$y], color = "NAME")) +
geom_point() +
labs(x = "Year", y = input$y) +
theme(text=element_text(size=10), axis.text.y=element_text(angle=90, hjust=1)) +
scale_color_discrete(name = "Regions"))
})