问题所在
一个接一个地绘制一堆线条图,但我只想在它们都在它们之间绘制之后才给它们上色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,1:35作为col.names,如果那个年份存在,每个单元格内部的值就是agg,如果不存在,它就是NA。同样,我相信这是可能的,我只是不能得到一个好的解决方案,而且我现在的方式不稳定。
发布于 2017-03-09 11:27:29
如果我没理解错的话,你想用一种颜色画出所有的线,然后用几种不同的颜色画几条线。您可以使用ggplot2,在两个数据帧上调用geom_line两次。第一次绘制所有城市数据时,不需要将线条映射到颜色。第二次只绘制目标城市的子集,并将线条映射到颜色。您将需要重新组织原始数据框架,并为目标城市设置数据框架的子集。在下面的代码中,我使用tidyr和dplyr来处理数据帧。
### 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作为第一个参数。它将创建名为Year的key列,该列将存储年份编号。年号是除City列之外的df数据框中每一列的列名。gather函数还将创建一个名为Value的列,该列将存储df数据框中除City列之外的每个列中的所有数值。最后,这个过程不涉及City列,所以使用-City告诉gather函数“不要转换来自City列的数据”。
### Subset df2, select the city of interest
df3 <- df2 %>%
# In this example, assuming that City 2 and City 3 are of interest
filter(City %in% c(2, 3))
### Plot the data
ggplot(data = df2, aes(x = Year, y = Value, group = factor(City))) +
# Plot all city data here in gray lines
geom_line(size = 1, color = "gray") +
# Plot target city data with colors
geom_line(data = df3,
aes(x = Year, y = Value, group = City, color = factor(City)),
size = 2) 结果图可以在这里看到:https://dl.dropboxusercontent.com/u/23652366/example_plot.png
https://stackoverflow.com/questions/42685754
复制相似问题