Search code examples
rggplot2bar-chartgeom-bar

R: barplot in ggplot2 where the height of each bar is given


library(ggplot2)
mydat <- data.frame(type = c("A", "B", "C"),
                    height = c(0.9, 0.3, 0.4))
ggplot(mydat, aes(x = type, y = height, fill = type)) +
  geom_bar()

Running the above code gives me the following error:

Error: stat_count() must not be used with a y aesthetic.

I would like to create a barplot with 3 bars: one for A, one for B, and one for C. The barplot's y-axis ranges from 0 to 1, and the height of the boxplots are 0.9, 0.3, and 0.4, respectively. Is it possible to construct this barplot using geom_bar where the height of each bar is already given?


Solution

  • Use geom_col :

    library(ggplot2)
    ggplot(mydat, aes(x = type, y = height)) + geom_col()
    

    enter image description here

    To use geom_bar you need to specify stat = 'identity'.

    ggplot(mydat, aes(x = type, y = height)) + geom_bar(stat = 'identity')