2017-03-09 27 views
1

的問題

繪製了一堆彼此的頂部線圖的,但我只想要的顏色10它們都繪製明確後彼此之間(以想象我的「目標」隨着時間的推移而變化,同時能夠觀察其背後的其他物體,因此,這樣的例子就像100條線圖一樣,但是我想特別着色5或10個,以便與尊重90個其他灰階的趨勢的R - ggplot每個唯一實例多個折線圖,隨着時間的推移

以下文章有一個相當不錯的圖像,我想複製,但肉稍微更多的骨頭,,除了我想MANY在這三種灰度背後,但是這三種都是我想要在前景中看到的突出城市。

我的原始數據是採用以下形式:

# 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,如果不是。我再次確信這是可能的,我無法獲得一個好的解決方案,而我目前的方式是不穩定的。

回答

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作爲第一個參數。它將創建名爲Yearkey列,該列將存儲年份編號。年份編號是df數據框中每列的列名,但City列除外。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

+0

你會推薦什麼,如果我有一個羣集指派1-10另一列,你將如何構建到您的清洗工藝? – bmc

+0

我想你仍然可以使用'tidyr'軟件包中的'gather'功能。像'City'列一樣,使用'gather'時刪除'Cluster'列。例如,聚集(年,價值,城市, - 集羣)' – www

+0

我很難理解這是幹什麼的:'df2 <- df %>%gather(Year,Value,-City)%>%mutate = as.numeric(Year))',你會如何用英語朗讀這段文字? – bmc

相關問題