r - ggplot随时间推移每个唯一实例的多个折线图

时间:2017-03-09 02:47:06

标签: r time ggplot2 line visualization

问题

将一堆线条图绘制在一起,但我只想在它们之间全部绘制之后专门为它们着色(以便可视化我的'目标'随着时间的推移而能够查看其后面的其他群众。所以这个例子就像100线图一样,但我想专门为其中的5个或10个颜色进行讨论,以讨论其他90个灰度级的趋势。

以下帖子有一个非常好的图像,我想要复制,但骨骼上的肉稍多,,除了我想要在这3个灰色背后的许多线条,但这3个是我突出显示的城市我想在前景中看到,按照说法。

我的原始数据如下:

# The unique identifier is a City-State combo, 
# there can be the same cities in 1 state or many. 
# Each state's year ranges from 1:35, but may not have
# all of the values available to us, but some are complete.

r1 <- c("city1" , "state1" , "year" , "population" , rnorm(11) , "2")
r2 <- c("city1" , "state2" , "year" , "population" , rnorm(11) , "3")
r3 <- c("city2" , "state1" , "year" , "population" , rnorm(11) , "2")
r4 <- c("city3" , "state2" , "year" , "population" , rnorm(11) , "1")
r5 <- c("city3" , "state2" , "year" , "population" , rnorm(11) , "7")

df <- data.frame(matrix(nrow = 5, ncol = 16))
df[1,] <- r1
df[2,] <- r2
df[3,] <- r3
df[4,] <- r4
df[5,] <- r5

names(df) <- c("City", "State", "Year", "Population", 1:11, "Cluster")

head(df)


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
# City | State | Year | Population  | ... 11 Variables ... | Cluster    #
# ----------------------------------------------------------------------#
# Each row is a city instance with these features ...                   #
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

但我认为以不同的方式查看数据可能会更好,所以我也采用以下格式。我不确定哪个更适合这个问题。

cols <- c(0:35)
rows <- c("unique_city1", "unique_city2","unique_city3","unique_city4","unique_city5")
r1 <- rnorm(35)
r2 <- rnorm(35)
r3 <- rnorm(35)
r4 <- rnorm(35)
r5 <- rnorm(35)

df <- data.frame(matrix(nrow = 5, ncol = 35))
df[1,] <- r1
df[2,] <- r2
df[3,] <- r3
df[4,] <- r4
df[5,] <- r5

names(df) <- cols
row.names(df) <- rows

head(df)


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
#                       Year1 Year2 .......... Year 35  #
# UniqueCityState1       VAL    NA  ..........  VAL     #
# UniqueCityState2       VAL    VAL ..........  NA      #
#         .                                             #
#         .                                             #
#         .                                             #
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

先前尝试

我尝试使用melt将数据转换为ggplot可以接受并随时间绘制每个城市的格式,但似乎没有任何效果。此外,我已经尝试创建自己的函数来遍历我的每个独特的城市状态组合到stack ggplots,其中有关于该主题的相当多的研究可用,但还没有。我不确定如何找到这些独特的城市状态对,并随着时间的推移绘制它们的集群值或任何数值。或者也许我正在寻找的是不可能的,我不确定。

思想?

编辑:有关数据结构的更多信息

> head(df)
        city state year population    stat1 stat2 stat3 stat4 stat5
1       BESSEMER     1    1      31509 0.3808436            0 0.63473928   2.8563268    9.5528262
2     BIRMINGHAM     1    1     282081 0.3119671            0 0.97489728   6.0266377    9.1321287
3 MOUNTAIN BROOK     1    1      18221 0.0000000            0 0.05488173   0.2744086    0.4390538
4      FAIRFIELD     1    1      12978 0.1541069            0 0.46232085   3.0050855    9.8628448
5     GARDENDALE     1    1       7828 0.2554931            0 0.00000000   0.7664793    1.2774655
6          LEEDS     1    1       7865 0.2542912            0 0.12714558   1.5257470   13.3502861
  stat6 stat6 stat7 stat8 stat9 cluster
1     26.976419     53.54026  5.712654                    0               0.2856327       9
2     35.670605     65.49183 11.982374                    0               0.4963113       9
3      6.311399     21.40387  1.426925                    0               0.1097635       3
4     21.266759     68.11527 11.480968                    0               1.0787487       9
5      6.770567     23.24987  3.960143                    0               0.0000000       3
6     24.157661     39.79657  4.450095                    0               1.5257470      15
    agg
1  99.93970
2 130.08675
3  30.02031
4 115.42611
5  36.28002
6  85.18754

最终我需要以row.names,1:35为col.names的独特城市形式,如果该年份存在,则每个单元格内的值为agg或{ {1}}如果不是。我再次确信这是可能的,我只是无法获得一个好的解决方案,而且我目前的方式是不稳定的。

1 个答案:

答案 0 :(得分:2)

如果我理解你的问题,你想用一种颜色绘制所有的线条,然后用几种不同的颜色绘制几条线条。您可以使用ggplot2,在两个数据框上调用geom_line两次。第一次绘制所有城市数据而不将线条映射到颜色。第二次绘制目标城市的子集并将颜色映射到颜色。您需要重新组织原始数据框并对目标城市的数据框进行子集化。在以下代码中,我使用tidyrdplyr来处理数据框。

### Set.seed to improve reproducibility
set.seed(123)

### Load package
library(tidyr)
library(dplyr)
library(ggplot2)

### Prepare example data frame 
r1 <- rnorm(35)
r2 <- rnorm(35)
r3 <- rnorm(35)
r4 <- rnorm(35)
r5 <- rnorm(35)

df <- data.frame(matrix(nrow = 5, ncol = 35))
df[1,] <- r1
df[2,] <- r2
df[3,] <- r3
df[4,] <- r4
df[5,] <- r5 

names(df) <- 1:35

df <- df %>% mutate(City = 1:5)

### Reorganize the data for plotting
df2 <- df %>%
  gather(Year, Value, -City) %>%
  mutate(Year = as.numeric(Year))

gather函数将df作为第一个参数。它将创建名为key的{​​{1}}列,该列将存储年份编号。年份编号是Year数据框中除df列以外的每列的列名称。 City函数还会创建一个名为gather的列,该列将存储除Value列之外的df数据框中每列的所有数值。最后,City列不会涉及此过程,因此请使用City告诉-City函数&#34;不要转换gather列中的数据&#34 ;

City

可以在此处看到结果图:https://dl.dropboxusercontent.com/u/23652366/example_plot.png

相关问题