Search code examples
rggplot2geom-bar

Why is my bar plot showing an empty output?


I am trying to create a simple bar plot but the output is empty (picture attached) for some reason.

#test graph
test1 <- ggplot(leg19.votosglobais, aes(partido, votos), geom_bar(stat = "identity"))

The dataset is this:

> leg19.votosglobais
   partido   votos
1       ps 1908036
2      psd 1457704
3       be  500017
4  pcp-pev  332473
5   cds-pp  221774
6      pan  174511
7       ch   67826
8       il   67681
9        l   57172
10       a   40487
11  outros  167727
12 brancos  131704
13   nulos  123882

Any suggestions for how to fix this please?

test1


Solution

  • There are two issues - 1) stat would be "identity", 2), the aes is not closed and geom_bar is another layer

    library(ggplot2)
    ggplot(leg19.votosglobais, aes(partido, votos)) + 
            geom_bar(stat="identity")
    

    -output

    enter image description here

    data

    leg19.votosglobais <- structure(list(partido = c("ps", "psd", "be", "pcp-pev", "cds-pp", 
    "pan", "ch", "il", "l", "a", "outros", "brancos", "nulos"), votos = c(1908036L, 
    1457704L, 500017L, 332473L, 221774L, 174511L, 67826L, 67681L, 
    57172L, 40487L, 167727L, 131704L, 123882L)), class = "data.frame",
    row.names = c("1", 
    "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"
    ))