R:将"Pivot_Long&Quot;替换为"融化&Quot; [英] R: replacing "pivot_longer" with "melt"

查看:16
本文介绍了R:将"Pivot_Long&Quot;替换为"融化&Quot;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在上一个问题(R: Adding Two Series to a Graph)中,我学习了如何绘制以下图表:

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()

我的问题:有没有人知道是否可以使用基数R中的函数或reshape/reshape2中的函数来替换上面代码中的pivot_longer函数?

以下是我的尝试:

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函数(这确保在进入melt之前删除var_1_colvar_2_col))。然后,我们可以为透视数据指定两个新的列名(即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()

这篇关于R:将"Pivot_Long&Quot;替换为"融化&Quot;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆