【问题标题】:Graph that consider the columns of a matrix as series将矩阵的列视为系列的图
【发布时间】:2013-12-25 21:50:10
【问题描述】:

大家好,我正在使用 R 中的一个矩阵。这个名为 Matriz.Colocacion 的矩阵有五列,每列显示 2009、2010、2011、2012 和 2013 不同年份的信息。Matriz.Colocacion 的行名有月份每年。我的矩阵有下一种形式(我在最后一部分中包含了Matriz.Colocaciondput() 版本):

                 2009       2010       2011       2012       2013
enero       710004.35 1458624.41 6229245.09 4407422.89 3006568.05
febrero     889398.08  942099.60 5553163.01 4248144.11 2615730.00
marzo      1114883.11 1210951.20 6372919.90 3537103.40 2833299.10
abril      1419242.11 1151423.89 6755054.64 3500595.75 3438797.32
mayo       1585857.22 1598355.10 7119008.27 4049074.18 3224926.48
junio      1010455.56 1370856.78 7585411.87 3279868.96 2794029.57
julio      1292333.35 1420547.36 7402737.51 3420974.23 3003458.16
agosto     1032443.35 2048291.06 7250944.21 2602310.30 2486931.57
septiembre 1133260.11 3043637.60 6227706.73 2225635.25 2515076.46
octubre    1229593.84 3669634.09 5795989.01 2853467.41 2674568.38
noviembre  1074569.64 3641665.16 4015226.43 4178671.47       0.00
diciembre  1370905.58 6780879.37 5391952.87 2831027.32       0.00

我在尝试构建图表时遇到了问题。我不知道将我的矩阵分成单独的系列是否更好,或者是否有办法使用所有矩阵来绘制我想要的图。我想得到这样的图形:

x 轴显示row.names(Matriz.Colocacion),图例显示矩阵的不​​同年份(2009、2010、2011、2012、2013)。

我没有足够的 ggplot 知识,并且在尝试创建此图表时遇到了一些麻烦。我的矩阵的dput() 版本是下一个:

structure(c(710004.35, 889398.08, 1114883.11, 1419242.11, 1585857.22, 
1010455.56, 1292333.35, 1032443.35, 1133260.11, 1229593.84, 1074569.64, 
1370905.58, 1458624.41, 942099.6, 1210951.2, 1151423.89, 1598355.1, 
1370856.78, 1420547.36, 2048291.06, 3043637.6, 3669634.09, 3641665.16, 
6780879.37, 6229245.09, 5553163.01, 6372919.9, 6755054.64, 7119008.27, 
7585411.87, 7402737.51, 7250944.21, 6227706.73, 5795989.01, 4015226.43, 
5391952.87, 4407422.89, 4248144.11, 3537103.4, 3500595.75, 4049074.18, 
3279868.96, 3420974.23, 2602310.3, 2225635.25, 2853467.41, 4178671.47, 
2831027.32, 3006568.05, 2615730, 2833299.1, 3438797.32, 3224926.48, 
2794029.57, 3003458.16, 2486931.57, 2515076.46, 2674568.38, 0, 
0), .Dim = c(12L, 5L), .Dimnames = list(c("enero", "febrero", 
"marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", 
"octubre", "noviembre", "diciembre"), c("2009", "2010", "2011", 
"2012", "2013")))

我等着你可以帮助我。非常感谢。

【问题讨论】:

  • 您需要阅读一些关于ggplot 的基本文本,例如this。您会发现数据应该是“长”(而不是“宽”)格式。 this topic on SO 上有很多不错的帖子。
  • +1 表示出色的问题、示例数据以及您想要的清晰示例。

标签: r ggplot2


【解决方案1】:

诀窍是以正确的格式获取数据:

library(reshape2)
library(ggplot2)
library(scales)

dat_melt = melt(dat)
dat_melt = within(dat_melt, { 
   Var2 = as.factor(Var2) 
   Var1 = factor(Var1, levels = c('enero', 'febrero', 'marzo', 'abril', 'mayo', 'junio', 'julio', 'agosto', 'septiembre', 'octubre', 'noviembre', 'diciembre'))
  })

ggplot(dat_melt, aes(x = Var1, y = value, color = Var2, group = Var2)) + 
   geom_line() + 
   scale_y_continuous(labels = comma)

这个问题比我最初想象的要复杂一些。在这种情况下需要colorgroup 美学,因为x 轴是factor 变量,而ggplot2 通常在使用geom_line 时不会连接点,请参阅this link 了解更多信息.

【讨论】:

  • x 轴没有正确的顺序
  • 那是因为它是按字母顺序排列的。可以使用 factor 函数进行更改,更具体地说是 levels 参数。
  • 谢谢亲爱的@PaulHiemstra,但我怎样才能更正x轴的顺序,这对我来说是个麻烦!
  • 我添加了解决方案,请仔细阅读我使用的函数的文档以了解和了解正在发生的事情。
  • 非常感谢@PaulHiemstra 大师,在 y 轴上只有一个额外的问题,可能无法获得数字的科学记数法或数字的其他格式,也许有一些链接可以再次解决这个问题
猜你喜欢
  • 2018-07-25
  • 2018-01-03
  • 2013-05-17
  • 1970-01-01
  • 2021-08-23
  • 1970-01-01
  • 2018-01-09
  • 2016-08-26
  • 1970-01-01
相关资源
最近更新 更多