首页 > 解决方案 > 使用 R ggplot 绘制宽格式数据

问题描述

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

我是 R 新手,想用它ggplot来创建一个散点图来分析数据。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") 

在此处输入图像描述

标签: rdataframeggplot2

解决方案


您的数据是宽格式,因此最好将其转换为长格式以使用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 = Regiongroup = Region内部aes所以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 包(v0.2.1.9000)于 2018 年 10 月 12 日创建


推荐阅读