我有一个Shiny应用程序,它应该显示硅谷工人的数字(列count
)和race
之间的依赖关系。我希望当我选择左侧gender
时,图表中的job category
和company
会显示count
具有不同races
的工作人员。现在我的图表的计数范围仅从0到1,并显示不正确的图表。
这是我的代码:
library(shiny)
library(ggplot2)
library(dplyr)
bcl <- read.csv(file = "E:/country/data/reveal.csv", colClasses = c("character", "integer", "factor", "factor", "factor", "integer"), na.strings = c("na", "NA")) %>% na.omit()
ui <- fluidPage(
titlePanel("Silicon Valley Diversity Data"),
sidebarLayout(
sidebarPanel(
img(src = "silicon.png", height = 150, width = 250),br(),
em("Choose company, job category and gender"),br(),
radioButtons("genderInput", "gender",
choices = list("male" = "male",
"female" = "female"),
selected = "male"),
radioButtons("jobInput","Job category",
choices = c(
"First/Mid officials & Mgrs",
"Professionals",
"Administrative support",
"Sales workers"
),
selected = "Technicians"
),
selectInput("companyInput", "company",
choices = c("Adobe", "Cisco", "Facebook", "Google", "HP", "Intel", "Twitter"))
),
mainPanel(plotOutput("coolplot"),
br(), br(),
tableOutput("results"))
))
server <- function(input, output) {
output$coolplot <- renderPlot({
filtered <-
bcl %>%
filter(
gender == input$genderInput,
job_category == input$jobInput,
company == input$companyInput
)
ggplot(filtered, aes(race)) +
geom_bar(fill = "#9f3e74")
})
}
shinyApp(ui = ui, server = server)
这是我的应用:
答案 0 :(得分:0)
试试这个:
library(shiny)
library(ggplot2)
library(dplyr)
# Load the data
df <- read.csv(file = "reveal.csv",
colClasses = c("character", "integer", "factor", "factor", "factor", "integer"),
na.strings = c("na", "NA"))
# Clean the data
bcl <- df %>%
na.omit() %>%
select(-year) %>%
filter(count != 0) %>%
filter(job_category != "Totals",
job_category != "Previous_totals",
race != "Overall_totals",
race != "Two_or_more_races")
ui <- fluidPage(
titlePanel("Silicon Valley Diversity Data"),
sidebarLayout(
sidebarPanel(
img(
src = "silicon.png",
height = 150,
width = 250
),
br(),
em("Choose company, job category and gender"),
br(),
radioButtons(
"genderInput",
"gender",
choices = list("male" = "male",
"female" = "female"),
selected = "male"
),
selectInput(
"jobInput",
"Job category",
choices = unique(bcl$job_category) # get choices programmatically
),
selectInput(
"companyInput",
"company",
choices = unique(bcl$company) # get choices programmatically
)
),
mainPanel(plotOutput("coolplot"),
br(), br(),
tableOutput("results"))
)
)
server <- function(input, output) {
output$coolplot <- renderPlot({
filtered <- filter(
bcl,
company == input$companyInput,
gender == input$genderInput,
job_category == input$jobInput)
df <- filtered[rep(row.names(filtered), filtered$count), 1:4] # prepare data for plotting
ggplot(df, aes(race)) + geom_bar() + coord_flip()
})
}
shinyApp(ui = ui, server = server)
总之,解决方案涉及对数据进行更多清理,并在server
中执行过滤后进行一些最终操作。我也冒昧地在ui
做了一些改变。我希望你喜欢它。