creating a bar chart with shiny r
This works for me. Note that I have changed the data to my own sample data, and I assume there is a column 'year' to indicate wether data belongs to year 2000 or 2010. The reactive is subsequently used as input for your plotting function. I hope this helps to point you in the right direction.
data = data.frame(Population=sample(1:20,10),Households = sample(1:20,10), year=sample(c(2000,2010),10,replace=T))
ui <- fluidPage(
titlePanel(title = h4("Hillsborough County Population by Census", align="center")),
sidebarPanel(
radioButtons("YEAR", "Select the Census Year",
choices = c("2000", "2010"),
selected = "2000")),
mainPanel(
plotOutput("bar",height = 500))
)
server <- function(input,output){
reactive_data = reactive({
selected_year = as.numeric(input$YEAR)
return(data[data$year==selected_year,])
})
output$bar <- renderPlot({
color <- c("blue", "red")
our_data <- reactive_data()
barplot(colSums(our_data[,c("Population","Households")]),
ylab="Total",
xlab="Census Year",
names.arg = c("Population", "Households"),
col = color)
})
}
shinyApp(ui=ui, server=server)
Admin
Updated on June 04, 2022Comments
-
Admin almost 2 years
I am new to R and am looking for help. I have a csv with two rows (2000, 2010) and two columns (Population 997936 Households 391043 for 2000 and for 2010 1229226 and 474030 respectively). I am trying to create a reactive bar chart using shiny with radio buttons to select either 2000 or 2010 data and cannot get it to work. I know it is related to the way I filter but I cannot figure it out. Here is my code, I would sincerely appreciate any help. As you can see by the comments I have been trying A LOT.
library(shiny) data <- read.csv("hillsctypop.csv", header = TRUE, sep = ",", stringsAsFactors = FALSE) ui <- fluidPage( titlePanel(title = h4("Hillsborough County Population by Census", align="center")), sidebarPanel( radioButtons("YEAR", "Select the Census Year", choices = c("2000", "2010"), selected = "2000")), mainPanel( plotOutput("bar",height = 500)) ) server <- function(input,output){ #year = reactive(data({input$YEAR})) # filtered <- reactive({ #data %>% #filter(Type == input$year) #}) output$bar <- renderPlot({ # barplot(as.matrix(data)) # barplot(data()[,2,4,]) #x <- data[1, ] color <- c("blue", "red") barplot(as.integer(data$Population, data$Households), main = input$YEAR, ylab="Total", xlab="Census Year", names.arg = c("Population", "Households"), col = color) #legend("topright", legend = c("Population", "Households"), # fill = c("Blue", "red")) }) } shinyApp(ui=ui, server=server)
-
Admin almost 7 yearsThank you! Here is what my data looks like with year population and households as column headers 2000 997936 391043 2010 1229226 474030
-
Admin almost 7 yearsFlorian, I have two rows of data (2000, 2010) and three columns, year, population and households. the data is in a csv file in this format, 2000 997936 391043 and 2010 1229226 and 474030. How do I read in this data and execute the code above? I am sorry to ask such basic questions but I am a little confused.
-
Florian almost 7 yearsHi Joe, don't have my laptop available at the moment, but if not solved tomorrow I will give you an updated example.
-
Admin almost 7 yearsThank you!!! I was trying to read in my csv file and It gives me "incorrect number of names". When I do names(data) it comes back with > names(data) [1] "Year" "Population" "Households" "X" I think it is getting the "X" from a previous try of assigning data to that variable, so I am also trying to figure out how to delete it.
-
Florian almost 7 yearsI modified my answer, so it should work with your data. You can do data$X<-NULL to remove the X column. Please consider accepting my answer if you find it helpful.
-
Admin almost 7 yearsFlorian, I'm sorry, I do not see where your answer was changed to work with the data. I still see the sample data in the answer.
-
Admin almost 7 yearsHi Florian, I saw what you did on the colSums line. I used data <- read.csv("hillsctypop1.csv", header = TRUE, sep = ",", stringsAsFactors = FALSE) as the input and changed the colSums line to barplot(colSums(our_data[,c("Population","Households")]), and I get a chart with the Y axis from -1.0 to 1.0 with a straight line cross 0.0. I do not get a bar chart.
-
Admin almost 7 yearsFlorian, I got it working. Thanks so much for your help! I have accepted your answer.