【问题标题】:Plot lists of time series data for factors in R绘制 R 中因子的时间序列数据列表
【发布时间】:2021-12-07 06:50:39
【问题描述】:

我有一系列描述事件持续时间(以天为单位)的列表,我想将这些数据绘制成线条来比较这些列表。

以下是一些示例数据,说明在学校的哪一天提供了哪些午餐选择。我已经解析了我的数据,这是简化的形式。原来是复杂字符串的形式。

soup = c(15:18)
grilledcheese = c(0:19)
pasta = c(3:13)

我想创建一个与此类似的图表,x 轴为天,y 轴为 soupgrilled cheesepasta

我在网上查看过,但不确定要使用哪种图表。部分困难在于数据不是从 0 开始的,y 轴应该代表因子。

我尝试了什么:

我尝试在 ggplot 中绘制它,但它只需要数据帧。我想知道是否有办法直接从列表中绘制。似乎这里应该有一个简单的解决方案,也许我错过了。

我也试过了

plot(x = grilledcheese, y = rep(1, length(grilledcheese)))

这更接近我想要的,但我不确定如何在 y 轴上绘制多个因子。

【问题讨论】:

  • 你尝试了什么?你在哪一步挣扎?数据不在data.frame中是否有原因?你能提供一个minimal reproducible example吗?附:特别是如果这是一个家庭作业(它看起来很像一个;)你应该展示一些以前的工作see also here
  • 这实际上不是家庭作业,虽然我明白你为什么这么想。我是一名博士生,正在绘制有关患者疾病进展的临床数据,因此“烤奶酪”实际上可能对应于“发烧”或“头痛”。我从一些更复杂的字符串开始描述经历的症状,并将其简化为上述数字列表。该图像是 20 年前发布的图表,我正在尝试使用更新的数据重新创建该图表。我也会在原帖中提供更多信息。感谢您的回复以及您可以提供的任何帮助!

标签: r ggplot2 graph data-science data-visualization


【解决方案1】:

您首先需要将数据设计成数据框。你可以这样做,例如

soup = c(15:18)
grilledcheese = c(0:19)
pasta = c(3:13)

## make dataframe

library(tidyverse)
my_x_axis <- as_tibble(seq(0,20)) 
names(my_x_axis) <- 'x'
my_x_axis %>% mutate(soup_y = 1*ifelse(as.numeric(x %in% soup) == 1, 1, NA)) %>% 
              mutate(grilledcheese_y = 2*ifelse(as.numeric(x %in% grilledcheese) == 1, 1, NA)) %>% 
              mutate(pasta_y = 3*ifelse(as.numeric(x %in% pasta) == 1, 1, NA))  -> data

在这里,我知道您的 x 轴值介于 0 到 20 之间。您也可以选择它们,例如通过min(c(soup,grilledcheese,pasta))min(c(soup,grilledcheese,pasta)) 或其他一些逻辑。

按照this answer 的想法,我将三种食物的 y 轴位置硬编码为 1、2 和 3。

ggplot 命令读作:

library(ggplot2)              
ggplot() + 
  geom_line(data=data, aes(x = x, y=soup_y)) +
  geom_line(data=data, aes(x = x, y=grilledcheese_y)) +
  geom_line(data=data, aes(x = x, y=pasta_y)) + 
  scale_y_discrete(labels = NULL, breaks = NULL) + labs(y = "") +  ## drop y axis labels 
  scale_x_continuous(labels=seq(0,20,1), breaks=seq(0,20,1)) + # x axis tick marks
  geom_text(aes(label = c('soup','grilledcheese','pasta'), x = 0, y = c(1,2,3), vjust = -.2,hjust=-.3)) # add labels

【讨论】:

    【解决方案2】:

    首先,让我们用 ggplot2 将数据做成更易于处理的形状:

    library(tidyverse)    
    soup = c(15:18)
    grilledcheese = c(0:19)
    pasta = c(3:13)
    
    
    df <- data.frame(soup_min = c(min(soup),max(soup)),
                     grilledcheese = c(min(grilledcheese),max(grilledcheese)),
                     pasta = c(min(pasta),max(pasta)))
    
    df <- pivot_longer(df, cols = 1:3) %>% 
      group_by(name) %>% 
      mutate(minv = min(value),
             maxv = max(value)) %>% 
      ungroup() %>% 
      select(-value) %>% 
      distinct()
    

    数据

    # A tibble: 3 x 3
      name           minv  maxv
      <chr>         <int> <int>
    1 soup_min         15    18
    2 grilledcheese     0    19
    3 pasta             3    13
    

    图表

    然后我们可以绘制您想要的不同元素:每条线的起点和终点、线本身和轴主题。

    ggplot(df) +
      geom_segment(aes(x = minv, xend = maxv, y = name, yend = name)) +
      geom_point(aes(x = minv, y = name)) +
      geom_point(aes(x = maxv, y = name)) +
      scale_x_continuous(breaks = c(0:20),
                         labels = c(0:20),
                         limits = c(0,20),
                         expand = c(0,0)) +
      theme(axis.ticks.x = element_line(size = 1),
            axis.ticks.y = element_blank(),
            axis.ticks.length =unit(.25, "cm"),
            axis.line.x = element_line(size = 1),
            panel.background = element_blank()) +
      labs(x = "",
           y = "")
    

    我们得到这个:

    这应该可以解决问题。

    额外定制

    现在,如果您想在刻度之间添加刻度标签,您可能需要检查 here,因为您必须重新调整数据,并在获得所需的所有食物类型后完成图表.直到,我只是在标签中添加间距:

    ggplot(df) +
      geom_segment(aes(x = minv, xend = maxv, y = name, yend = name)) +
      geom_point(aes(x = minv, y = name)) +
      geom_point(aes(x = maxv, y = name)) +
      scale_x_continuous(breaks = c(0:20),
                         labels = paste("         ",0:20),
                         limits = c(0,20),
                         expand = c(0,0)) +
      theme(axis.ticks.x = element_line(size = 1),
            axis.ticks.y = element_blank(),
            axis.ticks.length =unit(.25, "cm"),
            axis.line.x = element_line(size = 1),
            panel.background = element_blank()) +
      labs(x = "",
           y = "")
    

    【讨论】:

      猜你喜欢
      • 2018-01-23
      • 2015-09-11
      • 1970-01-01
      • 2020-08-06
      • 1970-01-01
      • 1970-01-01
      • 2011-12-09
      • 1970-01-01
      • 2017-04-22
      相关资源
      最近更新 更多