在R中将matplot()转换为ggplot2函数?

时间:2015-09-16 19:14:44

标签: r matrix plot ggplot2

我正在尝试使用ggplot2包创建此图。目前,我知道如何仅使用matplot()函数制作此类图:

matplot(t(security_paths[[3]]), type='l')

Desired Graph

当前输入矩阵security_paths[[3]]是这样的,除了30列而不是15:

            V1        V2        V3        V4        V5        V6        V7        V8
result.2   100  99.37178  98.61707  98.98689  99.90287 100.04947  99.40548 101.40779
result.7   100 100.11730  99.59974  99.53214 100.19915 101.56142 101.75984 101.47623
result.2.1 100 100.50476 101.76885 102.49223 104.60058 104.77955 105.85920 105.75034
result.7.1 100 101.69973 101.41755 100.29977 100.24582 100.76930 100.92308 100.36429
result.2.2 100  98.53694 100.43020 100.44469 100.38406  99.97830  99.79855  99.11653
result.7.2 100  98.93675  97.90757  97.00043  97.97458  97.42952  95.74721  96.13461
result.2.3 100  98.80080  98.00636  99.07521  99.28543  99.87422 101.05531 100.83643
result.7.3 100 100.11288  99.70116 100.24362 100.40603 101.00101 101.00941 102.78160
result.2.4 100  99.34431  99.31093 100.05931  98.51813  98.16358  97.77950  98.66993
result.7.4 100 100.26578 101.02704 101.49346 102.44187 101.28351 101.22310 100.94201
                  V9       V10       V11       V12       V13       V14       V15
result.2   100.10401 100.06043 103.46456 105.82758 106.42934 108.01544 107.92772
result.7   102.92835 101.83701 100.98723 101.30874 102.94760 102.75872 103.40619
result.2.1 105.49117 106.64736 107.61651 108.06622 107.60332 107.10505 108.26500
result.7.1 101.06239 101.85163 103.28536 102.75925 101.97034 101.49260 100.95205
result.2.2  99.00879  97.64125  97.43550  96.66271  97.50641  96.30086  96.30059
result.7.2  95.99714  94.05973  95.44348  96.60735  94.97913  94.42340  93.57966
result.2.3 101.96880 103.12590 102.94677 104.25266 103.28532 101.56988 101.94995
result.7.3 102.68475 103.05124 103.07985 103.74680 103.46858 101.71314 100.02625
result.2.4  96.76381  97.58092  96.82826  96.00925  97.87556  98.61583  96.78436
result.7.4 100.37194  99.71654 100.32579 100.39055 100.44717 101.25647 101.86222

如何使用ggplot2中的绘图功能重新创建matplot图?

注意

给定的数据与图表不匹配,但格式相同。

1 个答案:

答案 0 :(得分:3)

我没有检查其他人是否使用了dplyr / tidyr(如果他们这样做了,有人会告诉我,我会删除这个并将问题标记为重复):

dat <- read.table(text="           V1        V2        V3        V4        V5        V6        V7        V8
result.2   100  99.37178  98.61707  98.98689  99.90287 100.04947  99.40548 101.40779
result.7   100 100.11730  99.59974  99.53214 100.19915 101.56142 101.75984 101.47623
result.2.1 100 100.50476 101.76885 102.49223 104.60058 104.77955 105.85920 105.75034
result.7.1 100 101.69973 101.41755 100.29977 100.24582 100.76930 100.92308 100.36429
result.2.2 100  98.53694 100.43020 100.44469 100.38406  99.97830  99.79855  99.11653
result.7.2 100  98.93675  97.90757  97.00043  97.97458  97.42952  95.74721  96.13461
result.2.3 100  98.80080  98.00636  99.07521  99.28543  99.87422 101.05531 100.83643
result.7.3 100 100.11288  99.70116 100.24362 100.40603 101.00101 101.00941 102.78160
result.2.4 100  99.34431  99.31093 100.05931  98.51813  98.16358  97.77950  98.66993
result.7.4 100 100.26578 101.02704 101.49346 102.44187 101.28351 101.22310 100.94201")

library(dplyr)
library(tidyr)
library(ggplot2)

dat %>% 
  add_rownames() %>% 
  gather(reading, value, -rowname) %>% 
  group_by(rowname) %>% 
  mutate(x=1:n()) %>% 
  ggplot(aes(x=x, y=value, group=rowname)) +
  geom_line(aes(color=rowname))

enter image description here