简体   繁体   中英

Barplot in R with year on the x axis

ive created a dataframe in R shown below

 df<- data.frame("Year" = c("2011-12", "2012-13", "2013-14", 
     "2014-15","2015-16", "2016-17"),
     "Average" = c(99.03,98.67,96.43,92.74,96.96,93.61) )

The error i get is found below:

Error in barplot.default(df): 'height' must be a vector or a matrix

I cant seem to figure out the correct code for a simple Bar plot with year on the X-axis and the average on the Y.

Would appreciate a bit of help. Thanks in advance.

I am not sure how are you using it but this seems to give what you need.

barplot(Average~Year, df)

在此处输入图像描述

We can use a named vector with barplot

barplot(setNames(df$Average, df$Year))

-output

在此处输入图像描述


Or another option is ggplot2

library(ggplot2)
ggplot(df, aes(x= Year, y = Average)) +
            geom_col()

-output 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM