R:用 "melt" 替换 "pivot_longer"

R: replacing "pivot_longer" with "melt"

在上一个问题()中,我学习了如何制作以下图表:

library(ggplot2)
library(dplyr)

my_data = data.frame(var_1_col = "red", var_2_col = "green", var_1 = rnorm(8,10,10), var_2 = rnorm(8,5,1), name = c("A", "B", "C", "D", "E", "F", "G", "H"))

  var_1_col var_2_col     var_1    var_2 name
1       red     green 14.726642 4.676161    A
2       red     green 11.011187 4.937376    B
3       red     green 12.418489 5.869617    C
4       red     green 21.935154 5.641106    D
5       red     green 20.209498 6.193123    E
6       red     green -5.339944 5.187093    F
7       red     green 20.540806 3.895683    G
8       red     green 21.619631 4.097438    H

my_data %>%
  select(-c(var_1_col, var_2_col)) %>%
  pivot_longer(-name, names_to = "variable", values_to = "value") %>%
  ggplot(., aes(x = name, y = value, label = name)) +
  geom_point(stat = 'identity', aes(color = variable), size = 6)  +
  scale_color_manual(
    name = "Var 1 or Var 2",
    labels = c("Var 1", "Var 2"),
    values = c("#00ba38", "#f8766d")
  ) +
  labs(title = "Plot",
       subtitle = "Plot: Dotplot") +
  coord_flip() +
  theme_bw()

我的问题:有谁知道是否可以使用 Base R 或 [=16] 中的函数替换上述代码中的 pivot_longer 函数=]/reshape2?

这是我的尝试:

library(reshape)
library(reshape2)

my_data %>%
  select(-c(var_1_col, var_2_col)) %>%
  reshape2::melt(my_data, id.vars = "name", measure.vars = c("var_1", "var_2")) %>%
  ggplot(., aes(x = name, y = value, label = name)) +
  geom_point(stat = 'identity', aes(color = variable), size = 6)  +
  scale_color_manual(
    name = "Var 1 or Var 2",
    labels = c("Var 1", "Var 2"),
    values = c("#00ba38", "#f8766d")
  ) +
  labs(title = "Plot",
       subtitle = "Plot: Dotplot") +
  coord_flip() +
  theme_bw()

但这会产生以下错误:

'names' attribute [7] must be the same length as the vector [3]

有人可以告诉我如何解决这个问题吗?

谢谢!

正如其他人提到的,pivot_longer 需要 tidyr(即 tidyr::pivot_longer)。然后,我们可以使用 .my_data 传递给melt 函数(这确保 var_1_colvar_2_col 在进入 melt 之前被删除)。然后,我们可以为透视数据指定两个新的列名(即, variablevalue).

library(reshape2)
library(tidyverse)

my_data %>%
  select(-c(var_1_col, var_2_col)) %>%
  melt(.,
       id.vars = "name",
       variable.name = "variable",
       value.name = "value") %>%
  ggplot(., aes(x = name, y = value, label = name)) +
  geom_point(stat = 'identity', aes(color = variable), size = 6)  +
  scale_color_manual(
    name = "Var 1 or Var 2",
    labels = c("Var 1", "Var 2"),
    values = c("#00ba38", "#f8766d")
  ) +
  labs(title = "Plot",
       subtitle = "Plot: Dotplot") +
  coord_flip() +
  theme_bw()

输出

或者你也可以像上面那样在measure.vars中指定变量,但是结果是一样的。

my_data %>%
  select(-c(var_1_col, var_2_col)) %>%
  melt(.,
       id.vars = "name",
       variable.name = "variable",
       measure.vars = c("var_1", "var_2")) %>%
  ggplot(., aes(x = name, y = value, label = name)) +
  geom_point(stat = 'identity', aes(color = variable), size = 6)  +
  scale_color_manual(
    name = "Var 1 or Var 2",
    labels = c("Var 1", "Var 2"),
    values = c("#00ba38", "#f8766d")
  ) +
  labs(title = "Plot",
       subtitle = "Plot: Dotplot") +
  coord_flip() +
  theme_bw()