【发布时间】:2023-03-08 19:19:01
【问题描述】:
我正在尝试在 R 中制作具有两个反应值的百分比条形图。下面是我正在使用的代码。我不断收到错误“找不到对象'种族'”和“找不到对象'性别'”。但是,“种族”和“性别”都在数据框“filtered_data()”中。你知道我该如何解决这个问题吗?
这是标签面板:
tabPanel(title = "Comparison Bar Plot",
h1("Bar Chart"),
h3("Adjust"),
selectInput(inputId = "AxisX", label = "X Axis Variable",
choices = c("Race", "Gender"),
selected = "Race"),
selectInput(inputId = "AxisY", label = "Y Axis Variable",
choices = c("Race", "Gender"),
selected = "Gender"),
plotlyOutput("comparisonbarplot")
)
这是输出:
output$comparisonbarplot <- renderPlotly({
filtered_data() %>% group_by(switch(input$AxisX, Race=race,Gender=gender), switch(input$AxisY, Race=race,Gender=gender)) %>%
summarise(count=n()) %>%
mutate(perc = count/sum(count)) %>%
ggplot(aes(x = switch(input$AxisX, Race=race,Gender=gender), y = perc*100, fill=switch(input$AxisY, Race=race,Gender=gender))) +
scale_fill_manual(values =cbPalette) +
geom_bar( stat="identity") +
xlab("Race/Ethnicity") +
ylab("Percent") +
labs(title = "Race/Ethnicity and Gender of Defendants \n in Dutchess County Courts Starting Fall 2018", fill = "Gender:")+
theme_economist() +
theme(plot.title = element_text(hjust = 0.5))
})
【问题讨论】:
标签: r shiny shiny-reactivity