R中的2个条形图

时间:2018-04-13 09:55:16

标签: r plot graph bar-chart

我正在尝试创建一个代表季节的月份图表,每个季节都会有不同的颜色与下面的图例。

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

和机场的质地不同。

Airports: EWR, JFK, LGA

我的数据是:

     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

我所做的是:

enter image description here

但我希望同一季节的月份颜色相同,机场颜色相同。

谢谢!

1 个答案:

答案 0 :(得分:0)

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)

enter image description here