简体   繁体   中英

2grouped bar plot in R

I am trying to create a graph with months that represent the seasons, that every seasons will be in different color with the legend below.

seasons: December, January, February- Winter
         March, April, May- Spring
         June, July, August- Summer
         September, October, November- Autumn 

and the the Airports will be in different texture.

Airports: EWR, JFK, LGA

My data is:

     1  2  3  4  5  6  7  8  9 10 11 12
  EWR 24 23 28 26 27 31 30 23 14 17 16 33
  JFK 16 21 20 21 22 28 32 23 14 13 12 25
  LGA 14 16 18 20 19 26 26 20 13 15 15 24

What I have made is:

在此处输入图片说明

but I want the months on the same seasons to be in the same color and the Airports to be in the same texture.

Thanks!

library(reshape2)
library(ggplot2)

df = data_frame(month = c(1, 2, 3,  4,  5,  6,  7,  8,  9, 10, 11, 12),
              EWR = c(24, 23, 28, 26, 27, 31, 30, 23, 14, 17, 16, 33), 
              JFK =c(16, 21, 20, 21, 22, 28, 32, 23, 14, 13, 12, 25),
              LGA = c(14, 16, 18, 20, 19, 26, 26, 20, 13, 15, 15, 24),
              season = c("Winter", "Winter", "Spring", "Spring", 
                         "Spring", "Summer", "Summer", "Summer", 
                         "Autumn", "Autumn", "Autumn", "Winter"))

df = melt(df, id.vars = c("month", "season"))

head(df)

     month season   variable value
1     1    Winter      EWR    24
2     2    Winter      EWR    23
3     3    Spring      EWR    28
4     4    Spring      EWR    26
5     5    Spring      EWR    27
6     6    Summer      EWR    31

ggplot(df, aes(x = as.factor(month), y = value, fill = season, col = season)) + 
geom_col(position = "dodge") + 
facet_grid(~variable)

在此处输入图片说明

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