【问题标题】:R: converting tidyverse to dplyr/reshape2 for plotsR:将 tidyverse 转换为 dplyr/reshape2 用于绘图
【发布时间】:2021-03-15 19:13:49
【问题描述】:

在上一篇文章中,一位用户向我展示了如何在 R 中绘制纵向数据。代码如下:

library(ggplot2)

Data <- data.frame(
    
    "ID" = c("ABC111", "ABC111", "ABC111", "ABC111", "ABC112", "ABC112", "ABC112", "ABC113", "ABC113", "ABC114", "ABC115"),
"color" = c("red", "red", "red", "red", "blue", "blue", "blue", "green", "green", "black", "yellow"),
    "start_date" = c("2005/01/01", "2006/01/01", "2007/01/01", "2008/01/01", "2009/01/01", "2010/01/01", "2011/01/01", "2012/01/01", "2013/01/01", "2014/01/01", "2015/01/01"),
    "end_date" = c("2005/09/01", "2006/06/01", "2007/04/01", "2008/05/07", "2009/06/01", "2010/10/01", "2011/12/12", "2013/05/01", "2013/06/08", "2015/01/01", "2016/08/09")
)

Data$ID = as.factor(Data$ID)
Data$color = as.factor(Data$color)

library(tidyverse)

Data %>%
  # Number each row in its order of appearance, 
  # save this numbers in a new column named order
  rowid_to_column("order") %>%
  # Change data from wide to long format
  pivot_longer(cols = c(start_date, end_date),
               names_to = "date_type",
               values_to = "date") %>%
  # Ggplot, use date as x, order as y, ID as col and order as group
  ggplot(aes(x = date, 
             y = order,  
             col = ID, 
             group = order)) +
  # Draw points
  geom_point()+
  # Draw lines
  geom_line() +
  # Maybe you want to remove the y axis title, text and ticks
  theme(axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        # I added a vertical format to the x axis labels 
        # it might easier to read this way
        axis.text.x = element_text(angle = 90, vjust = 0.5))

此解决方案需要“tidyverse”库。我用于工作的计算机没有 USB 端口或互联网连接,它只有 R 并安装了一些软件包(例如 dplyr、ggplot2、reshape2)。这段代码可以改成使用“dplyr”和“reshape2”而不是“tidyverse”吗?

我尝试了以下代码(在之前的帖子中向我推荐过):

Data %>%
    # Number each row in its order of appearance, 
    # save this numbers in a new column named order
    mutate(order = row_number()) %>%
    # Change data from wide to long format
    melt(cols = c(start_date, end_date),
         names_to = "date_type",
         values_to = "date") %>%
    # Ggplot, use date as x, order as y, ID as col and order as group
    ggplot(aes(x = date, 
               y = order,  
               col = ID, 
               group = order)) +
    # Draw points
    geom_point()+
    # Draw lines
    geom_line() +
    # Maybe you want to remove the y axis title, text and ticks
    theme(axis.title.y = element_blank(),
          axis.text.y = element_blank(),
          axis.ticks.y = element_blank(),
          # I added a vertical format to the x axis labels 
          # it might easier to read this way
          axis.text.x = element_text(angle = 90, vjust = 0.5))

但我收到以下错误:

Using ID, color, start_date, end_date as id variables
Don't know how to automatically pick scale for object of type function. Defaulting to continuous.
Don't know how to automatically pick scale for object of type function. Defaulting to continuous.
Don't know how to automatically pick scale for object of type function. Defaulting to continuous.
Error: Aesthetics must be valid data columns. Problematic aesthetic(s): x = date, y = order, group = order. 

谁能告诉我我做错了什么?

谢谢

上一篇文章的链接:R: connect points on a graph (ggplot2)

【问题讨论】:

  • melt()pivot_longer() 具有相似的效果,但语法和参数名称不同。您需要使用参数measure.vars 而不是cols,并使用字符串作为列名而不是整洁的评估。 variable.name 对应于names_tovalue.name 对应于values_to。除此之外,我认为您使用的所有功能都在 dplyr 和 ggplot2 中,因此不需要进行其他更改。

标签: r ggplot2 dplyr data-visualization


【解决方案1】:

按照 @qdread 和一些 base R 的精彩评论的建议,仅使用 reshape2 中的 melt()

library(reshape2)
library(ggplot2)
#Code
Data$order <- rownames(Data)
Melted <- melt(Data,id.vars = c('order','ID','color'))
Melted$order <- as.numeric(Melted$order)
#Plot
G <- ggplot(Melted,aes(x = value, 
           y = order,  
           col = ID, 
           group = order)) +
  # Draw points
  geom_point()+
  # Draw lines
  geom_line() +
  # Maybe you want to remove the y axis title, text and ticks
  theme(axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        # I added a vertical format to the x axis labels 
        # it might easier to read this way
        axis.text.x = element_text(angle = 90, vjust = 0.5))

输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-18
    • 2017-09-21
    • 2021-07-23
    • 1970-01-01
    • 2022-01-15
    • 2020-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多