【问题标题】:r - ggplot multiple line graphs for each unique instance over timer - 随着时间的推移,每个唯一实例的 ggplot 多条线图
【发布时间】: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


    【解决方案1】:

    如果我正确理解您的问题,您想用一种颜色绘制所有线条,然后用几种不同颜色绘制几条线条。您可以使用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

    【讨论】:

    • 如果我有另一列用于集群分配 1-10,您会推荐什么,您将如何将其纳入您的清洁过程?
    • 我猜你仍然可以使用 tidyr 包中的 gather 函数。与City 列一样,在使用gather 时删除Cluster 列。例如,gather(Year, Value, -City, -Cluster)
    • 我很难理解这是在做什么:df2 &lt;- df %&gt;% gather(Year, Value, -City) %&gt;% mutate(Year = as.numeric(Year)),你如何用英语大声朗读这个?
    • @bmc 请在我的回答中查看更新后的描述。您还可以在此处 (r4ds.had.co.nz/tidy-data.html) 了解有关 tidyr 包和 gather 功能的更多信息。在此处 (r4ds.had.co.nz/pipes.html) 了解有关管道 (%&gt;%) 的更多信息,在此处 (cran.rstudio.com/web/packages/dplyr/vignettes/introduction.html) 了解有关包 dplyr 的更多信息。
    • 我的问题是始终将数据转换为第二种格式。你有没有一个好方法可以从示例中获取我的第一种格式并将其转换为第二种格式?
    猜你喜欢
    • 1970-01-01
    • 2020-10-01
    • 2012-10-06
    • 1970-01-01
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-24
    相关资源
    最近更新 更多