我正在尝试创建一个反应式绘图,您可以在其中选择selectInput()中的种族,并查看中西部的该种族的人口。
这是我的用户界面:
ethnicity_sidebar_content <- sidebarPanel(
selectInput(
inputId = "ethnicity",
label = "Select Ethnicity",
choices = list(
"Total" = "total",
"White" = "white",
"Black" = "black",
"American Indian" = "amerindian",
"Asian" = "asian",
"Other" = "other"
)
)
)
ethnicity_main_content <- mainPanel(
plotOutput("ethnicity_plot")
)
ethnicity_panel <- tabPanel(
"Midwest by Ethnicity",
sidebarLayout(
ethnicity_sidebar_content,
ethnicity_main_content
)
)
这是我的服务器:
midwest_poverty <- midwest %>%
mutate(popbelowpoverty = floor(percbelowpoverty / 100 * poppovertyknown)) %>%
group_by(state) %>%
summarise(
poppovertyknown = sum(poppovertyknown),
popbelowpoverty = sum(popbelowpoverty)
) %>%
mutate(popabovepoverty = poppovertyknown - popbelowpoverty)
server <- function(input, output) {
output$ethnicity_plot <- renderPlot({
p <- ggplot(data = midwest_ethnicity) +
geom_bar(
mapping = aes(x = state, y = input$ethnicity),
stat = "identity"
)
p
})
}
当我运行ShinyApp时,我会不断获得条形图,该条形图绘制列名而不是列中的数据。
编辑:我认为这是一个简单的错误,我使用aes而不是aes_string
答案 0 :(得分:0)
在ggplot调用中写入aes(x = state, y = input$ethnicity)
时,它将在数据集state
中为x轴查找变量midwest_ethnicity
。与y相同,例如,如果这是input$ethnicity
中的值,它将查找名为White的变量。
我认为您的数据集中没有这样名称的变量。