使用R ggplot绘制宽格式数据

时间:2018-10-13 04:30:27

标签: r dataframe ggplot2

我有一个数据框(如下所示),按地区显示了年销售额。最后一列计算三年内该地区所有销售额的总和。

我是R新手,想使用ggplot创建一个SINGLE散点图来分析数据。 x轴为三年,y轴为销售额。

理想情况下,每个区域在2013、2014、2015和2016年都有自己的点线(除了几个NA)。然后,我想根据区域为每条线上色。总和列不应出现在绘图上。有什么想法吗?

df <- structure(list(Region = structure(1:6, 
                                  .Label = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", 
                                             "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U"), 
                                  class = "factor"), 
               "2016" = c(8758.82, 25559.89, 30848.02, 8696.99, 3621.12, 5468.76), 
               "2015" = c(26521.67, 89544.93, 92825.55, 28916.4, 14004.54, 16618.38), 
               "2014" = c(NA, NA, 199673.73, 37108.09, 16909.87, 20610.58), 
               "2013" = c(27605.35, NA, 78794.31, 31824.75, 17990.21, 17307.11), 
               "Total Sales" = c(35280.49, 115104.82, 323347.3, 74721.48, 34535.53, 42697.72)), 
          row.names = c(NA, 6L), class = "data.frame") 

enter image description here

1 个答案:

答案 0 :(得分:2)

您的数据采用宽格式,因此最好将其转换为长格式以与ggplot一起使用。在这里,我用tidyr::gather()来完成

library(tidyr)
library(ggplot2)

df_long <- df %>% 
  gather(Year, Sales, -Region)
df_long
#>    Region        Year     Sales
#> 1       A        2016   8758.82
#> 2       B        2016  25559.89
#> 3       C        2016  30848.02
#> 4       D        2016   8696.99
#> 5       E        2016   3621.12
#> 6       F        2016   5468.76
#> 7       A        2015  26521.67
#> 8       B        2015  89544.93
#> 9       C        2015  92825.55
#> 10      D        2015  28916.40
#> 11      E        2015  14004.54
#> 12      F        2015  16618.38
#> 13      A        2014        NA
#> 14      B        2014        NA
#> 15      C        2014 199673.73
#> 16      D        2014  37108.09
#> 17      E        2014  16909.87
#> 18      F        2014  20610.58
#> 19      A        2013  27605.35
#> 20      B        2013        NA
#> 21      C        2013  78794.31
#> 22      D        2013  31824.75
#> 23      E        2013  17990.21
#> 24      F        2013  17307.11
#> 25      A Total Sales  35280.49
#> 26      B Total Sales 115104.82
#> 27      C Total Sales 323347.30
#> 28      D Total Sales  74721.48
#> 29      E Total Sales  34535.53
#> 30      F Total Sales  42697.72

绘图:在color = Region内指定group = Regionaes,以便ggplot知道如何选择颜色并绘制线条

ggplot(df_long, aes(x = Year, y = Sales, color = Region, group = Region)) +
  geom_point() +
  geom_line() +
  scale_color_brewer(palette = 'Dark2') +
  theme_classic(base_size = 12)
#> Warning: Removed 3 rows containing missing values (geom_point).
#> Warning: Removed 2 rows containing missing values (geom_path).

也可以使用facet_grid()

ggplot(df_long, aes(x = Year, y = Sales, group = Region)) +
  geom_point() +
  geom_line() +
  facet_grid(Region ~., scales = 'free_y') +
  theme_bw(base_size = 12)
#> Warning: Removed 3 rows containing missing values (geom_point).
#> Warning: Removed 2 rows containing missing values (geom_path).

reprex package(v0.2.1.9000)于2018-10-12创建