【发布时间】:2017-07-29 20:55:53
【问题描述】:
问题
在彼此之上绘制一堆线图,但我只想在它们相互绘制之后专门为 10 着色(以可视化我的“目标”如何随着时间的推移而移动,同时能够查看其他在它们后面。因此,一个例子就像 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 的独特城市的形式,col.names 的 1:35 和每个单元格内的值是 agg 如果那一年存在或 NA 如果它不是吨。我再次确信这是可能的,我只是无法找到一个好的解决方案,而且我目前的方法不稳定。
【问题讨论】:
标签: r time ggplot2 line visualization