Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make a bar plot showing different months in R

I have this data set.

##          Jan        Feb        Mar       Apr       May       Jun       Jul
## 1 0.05266167 0.11384153 0.14253802 0.1507699 0.1571320 0.1121953 0.1647502
## 2 0.09274933 0.11954029 0.17114252 0.1644035 0.2030245 0.2571798 0.1824315
## 3 0.12873033 0.15829162 0.19940906 0.1804324 0.2239007 0.2279327 0.2416183
## 4 0.06973417 0.05635917 0.06897862 0.1149706 0.1121177 0.1182867 0.1173400
## 5 0.15117362 0.10942123 0.12451943 0.1702807 0.2135427 0.2164273 0.2075777
## 6 0.41753005 0.34718628 0.29915414 0.2856211 0.4458226 0.5053402 0.6380533
## 7 0.43642524 0.37800174 0.27653371 0.3551805 0.3233486 0.3996235 0.4975522
## 8 0.21988812 0.22843585 0.23984762 0.2705450 0.2058714 0.2185158 0.2564072
##          Aug       Sep       Oct        Nov        Dec Units     Annual
## 1 0.19781433 0.1566800 0.1901342 0.09750671 0.08782510   m/s 0.14005536
## 2 0.27276717 0.2393716 0.1152078 0.09969814 0.09888717   m/s 0.17627843
## 3 0.25592567 0.2641575 0.1358387 0.17679250 0.15574617   m/s 0.19947550
## 4 0.06964667 0.1165557 0.0852325 0.08210633 0.06016383   m/s 0.09029416
## 5 0.15983400 0.1917124 0.2359582 0.15190757 0.14265571   m/s 0.17288818
## 6 0.48610683 0.4169365 0.4343557 0.36020295 0.48484231   m/s 0.41894140
## 7 0.43456983 0.3914535 0.3669773 0.36234052 0.40674304   m/s 0.38100448
## 8 0.31760617 0.3240705 0.3552170 0.25114412 0.22125319   m/s 0.26848028
##   Direction RecpDir       Code      Means
## 1         N       S 0.03335250 0.13532074
## 2        NE      SW 0.07719650 0.16803361
## 3        E        W 0.07318150 0.19573130
## 4        SE      NW 0.05302025 0.08929099
## 5        S        N 0.02574475 0.17291754
## 6        SW      NE 0.03034450 0.42676267
## 7        W        E 0.03523275 0.38572913
## 8        NW      SE 0.01729375 0.25906683

I need to make a bar plot that shows the wind velocity for January through December. Each month has 8 rows of data that need to be combined into 1 using the ‘mean’ function. How would I get the mean for each month and then turn this into a bar plot? I need the x-axis to have each month labeled, with an axis title of "Month" and the y-axis title to be "Velocity (m/s)". I have included the data below.

 structure(list(Jan = c(0.0526616666666667, 0.0927493333333333, 
 0.128730333333333, 0.0697341666666667, 0.151173619047619, 0.417530047619048, 
 0.436425238095238, 0.219888119047619), Feb = c(0.113841531746032, 
 0.119540285714286, 0.158291619047619, 0.0563591666666667, 0.10942123015873, 
 0.347186277777778, 0.378001738095238, 0.228435849206349), Mar = c(0.142538023809524, 
 0.171142523809524, 0.199409055555556, 0.0689786190476191, 0.124519428571429, 
 0.299154142857143, 0.276533714285714, 0.239847619047619), Apr = c(0.150769928571429, 
 0.164403476190476, 0.180432404761905, 0.114970595238095, 0.170280738095238, 
 0.285621119047619, 0.355180452380952, 0.270545), May = c(0.15713196969697, 
 0.203024523809524, 0.223900714285714, 0.112117666666667, 0.213542666666667, 
 0.445822642857143, 0.323348571428571, 0.205871396103896), Jun = c(0.11219530952381, 
 0.25717980952381, 0.227932666666667, 0.118286666666667, 0.216427333333333, 
 0.505340214285714, 0.3996235, 0.218515833333333), Jul = c(0.164750166666667, 
 0.1824315, 0.241618333333333, 0.11734, 0.207577666666667, 0.638053333333333, 
 0.497552166666667, 0.256407166666667), Aug = c(0.197814333333333, 
 0.272767166666667, 0.255925666666667, 0.0696466666666667, 0.159834, 
 0.486106833333333, 0.434569833333333, 0.317606166666667), Sep = c(0.156680025974026, 
 0.239371555555556, 0.26415753030303, 0.116555720779221, 0.191712357142857, 
 0.4169365, 0.3914535, 0.3240705), Oct = c(0.190134166666667, 
 0.115207833333333, 0.135838666666667, 0.0852325, 0.235958166666667, 
 0.434355666666667, 0.366977333333333, 0.355217047619048), Nov = c(0.0975067142857143, 
 0.0996981428571428, 0.1767925, 0.0821063333333333, 0.151907571428571, 
 0.360202952380952, 0.362340523809524, 0.251144119047619), Dec = c(0.0878250952380952, 
 0.0988871666666667, 0.155746166666667, 0.0601638333333333, 0.142655714285714, 
 0.484842311688312, 0.40674303968254, 0.22125319047619), Units = c("m/s", 
 "m/s", "m/s", "m/s", "m/s", "m/s", "m/s", "m/s"), Annual = c(0.140055362193362, 
 0.176278428571429, 0.199475503607504, 0.0902941623376623, 0.172888182539683, 
 0.418941396825397, 0.381004476190476, 0.268480282828283), Direction = c("N", 
 "NE", "E ", "SE", "S ", "SW", "W ", "NW"), RecpDir = c("S", "SW", 
 "W", "NW", "N", "NE", "E", "SE"), Code = c(0.0333525, 0.0771965, 
 0.0731815, 0.05302025, 0.02574475, 0.0303445, 0.03523275, 0.01729375
 ), Means = c(0.135320744348244, 0.16803360978836, 0.19573130477393, 
 0.0892909945887446, 0.172917541005291, 0.42676267015392, 0.385729134259259, 
 0.259066833934584)), row.names = c(NA, 8L), class = "data.frame")

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Data reshaping, then plots.

Base R

datlong <- reshape2::melt(dat[,1:12], id.vars=c(),  variable.name = "month")
datlong[c(1:3, 94:96),]
#    month      value
# 1    Jan 0.05266167
# 2    Jan 0.09274933
# 3    Jan 0.12873033
# 94   Dec 0.48484231
# 95   Dec 0.40674304
# 96   Dec 0.22125319
datagg <- aggregate(value ~ month, FUN = mean, data = datlong)
head(datagg)
#   month     value
# 1   Jan 0.1961116
# 2   Feb 0.1888847
# 3   Mar 0.1902654
# 4   Apr 0.2115255
# 5   May 0.2355950
# 6   Jun 0.2569377
month.abb
#  [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
datagg$month <- factor(datagg$month, levels = month.abb)

Simple barplot:

barplot(value ~ month, data = datagg)

base graphics barplot

dplyr and ggplot2

library(dplyr)
library(tidyr) # pivot_longer
library(ggplot2)
dat %>%
  select(Jan:Dec) %>%
  pivot_longer(everything(), names_to = "month") %>%
  mutate(month = factor(month, levels = month.abb)) %>% 
  group_by(month) %>%
  summarize(value = mean(value)) %>%
  ggplot(aes(month, value)) +
  geom_bar(stat = "identity")

ggplot2 barplot


Data

dat <- structure(list(Jan = c(0.0526616666666667, 0.0927493333333333, 0.128730333333333, 0.0697341666666667, 0.151173619047619, 0.417530047619048, 0.436425238095238, 0.219888119047619), Feb = c(0.113841531746032, 0.119540285714286, 0.158291619047619, 0.0563591666666667, 0.10942123015873, 0.347186277777778, 0.378001738095238, 0.228435849206349), Mar = c(0.142538023809524, 0.171142523809524, 0.199409055555556, 0.0689786190476191, 0.124519428571429, 0.299154142857143, 0.276533714285714, 0.239847619047619),      Apr = c(0.150769928571429, 0.164403476190476, 0.180432404761905, 0.114970595238095, 0.170280738095238, 0.285621119047619, 0.355180452380952, 0.270545), May = c(0.15713196969697, 0.203024523809524, 0.223900714285714, 0.112117666666667, 0.213542666666667, 0.445822642857143, 0.323348571428571, 0.205871396103896), Jun = c(0.11219530952381, 0.25717980952381, 0.227932666666667, 0.118286666666667, 0.216427333333333, 0.505340214285714, 0.3996235, 0.218515833333333), Jul = c(0.164750166666667, 0.1824315,      0.241618333333333, 0.11734, 0.207577666666667, 0.638053333333333, 0.497552166666667, 0.256407166666667), Aug = c(0.197814333333333, 0.272767166666667, 0.255925666666667, 0.0696466666666667, 0.159834, 0.486106833333333, 0.434569833333333, 0.317606166666667), Sep = c(0.156680025974026, 0.239371555555556, 0.26415753030303, 0.116555720779221, 0.191712357142857, 0.4169365, 0.3914535, 0.3240705), Oct = c(0.190134166666667, 0.115207833333333, 0.135838666666667, 0.0852325, 0.235958166666667, 0.434355666666667,      0.366977333333333, 0.355217047619048), Nov = c(0.0975067142857143, 0.0996981428571428, 0.1767925, 0.0821063333333333, 0.151907571428571, 0.360202952380952, 0.362340523809524, 0.251144119047619), Dec = c(0.0878250952380952, 0.0988871666666667, 0.155746166666667, 0.0601638333333333, 0.142655714285714, 0.484842311688312, 0.40674303968254, 0.22125319047619), Units = c("m/s", "m/s", "m/s", "m/s", "m/s", "m/s", "m/s", "m/s"), Annual = c(0.140055362193362, 0.176278428571429, 0.199475503607504, 0.0902941623376623,      0.172888182539683, 0.418941396825397, 0.381004476190476, 0.268480282828283), Direction = c("N", "NE", "E ", "SE", "S ", "SW", "W ", "NW"), RecpDir = c("S", "SW", "W", "NW", "N", "NE", "E", "SE"), Code = c(0.0333525, 0.0771965, 0.0731815, 0.05302025, 0.02574475, 0.0303445, 0.03523275, 0.01729375), Means = c(0.135320744348244, 0.16803360978836, 0.19573130477393, 0.0892909945887446, 0.172917541005291, 0.42676267015392, 0.385729134259259, 0.259066833934584)), row.names = c(NA, 8L), class = "data.frame")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading