Pivot_longer 错误(由于类别不同而无法合并)

发布于 2025-01-18 16:43:37 字数 769 浏览 0 评论 0原文

我想绘制带有以下数据集的条形图。 x轴是“输入”类型和'rtype'

df <- read.table(text = "       Input Rtype Rcost Rsolutions  Btime Bcost 
1   12-proc.     typea    36     614425     40    36 
2   15-proc.     typeb    51     534037     50    51 
3    18-proc     typec    62    1843820     66    66 
4    20-proc     typea    68    1645581 104400    73 
5 20-proc(l)     typeb    64    1658509  14400    65 
6    21-proc     typec    78    3923623 453600    82", 
header = TRUE,sep = "")

dfm <- pivot_longer(df, -Input, names_to="variable", values_to="value")

数据集的图像

但是,我得到此错误:错误:无法组合输入&lt; factor&gt;和rtype

请建议,谢谢!

I want to plot a bar graph with the following dataset.
With the X-axis being the 'Input' types and 'Rtype'

df <- read.table(text = "       Input Rtype Rcost Rsolutions  Btime Bcost 
1   12-proc.     typea    36     614425     40    36 
2   15-proc.     typeb    51     534037     50    51 
3    18-proc     typec    62    1843820     66    66 
4    20-proc     typea    68    1645581 104400    73 
5 20-proc(l)     typeb    64    1658509  14400    65 
6    21-proc     typec    78    3923623 453600    82", 
header = TRUE,sep = "")

dfm <- pivot_longer(df, -Input, names_to="variable", values_to="value")

Image of dataset

However, i get this error: Error: Can't combine Input <factor> and Rtype .

Please advise, thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残龙傲雪 2025-01-25 16:43:37

也许这会有所帮助:

library(tidyverse)

df <- read.table(text = "       Input Rtype Rcost Rsolutions  Btime Bcost 
1   12-proc.     typea    36     614425     40    36 
2   15-proc.     typeb    51     534037     50    51 
3    18-proc     typec    62    1843820     66    66 
4    20-proc     typea    68    1645581 104400    73 
5 20-proc(l)     typeb    64    1658509  14400    65 
6    21-proc     typec    78    3923623 453600    82", 
header = TRUE,sep = "")

dfm <- pivot_longer(df, -c(Input, Rtype), names_to="variable", values_to="value")
dfm
#> # A tibble: 24 × 4
#>    Input    Rtype variable     value
#>    <chr>    <chr> <chr>        <int>
#>  1 12-proc. typea Rcost           36
#>  2 12-proc. typea Rsolutions  614425
#>  3 12-proc. typea Btime           40
#>  4 12-proc. typea Bcost           36
#>  5 15-proc. typeb Rcost           51
#>  6 15-proc. typeb Rsolutions  534037
#>  7 15-proc. typeb Btime           50
#>  8 15-proc. typeb Bcost           51
#>  9 18-proc  typec Rcost           62
#> 10 18-proc  typec Rsolutions 1843820
#> # … with 14 more rows

# If you have all 18 factor levels in your data:
ggplot(dfm, aes(x = interaction(Input, Rtype, sep = " "),
                y = value, fill = variable)) +
  geom_bar(stat = "identity") +
  scale_y_log10(labels = scales::dollar,
                name = "log10(cost)") +
  scale_fill_viridis_d(end = 0.8) +
  theme(axis.title.x = element_blank(),
        axis.text.x = element_text(angle = 90, vjust = 0.5))

# If you don't have all 18 factor levels in your data:
all_combinations <- expand_grid(Input = dfm$Input,
                                Rtype = dfm$Rtype) %>%
  distinct()

dfm_expanded <- left_join(all_combinations, dfm) %>%
  replace_na(list("0"))
#> Joining, by = c("Input", "Rtype")

ggplot(dfm_expanded, aes(x = interaction(Input, Rtype, sep = " "),
                         y = value, fill = variable)) +
  geom_bar(stat = "identity") +
  scale_y_log10(labels = scales::dollar,
                name = "log10(cost)") +
  scale_fill_viridis_d(end = 0.8) +
  theme(axis.title.x = element_blank(),
        axis.text.x = element_text(angle = 90, vjust = 0.5))
#> Warning: Removed 12 rows containing missing values (position_stack).

reprex 软件包于 2022 年 4 月 4 日创建 (v2.0.1)

Perhaps this will help:

library(tidyverse)

df <- read.table(text = "       Input Rtype Rcost Rsolutions  Btime Bcost 
1   12-proc.     typea    36     614425     40    36 
2   15-proc.     typeb    51     534037     50    51 
3    18-proc     typec    62    1843820     66    66 
4    20-proc     typea    68    1645581 104400    73 
5 20-proc(l)     typeb    64    1658509  14400    65 
6    21-proc     typec    78    3923623 453600    82", 
header = TRUE,sep = "")

dfm <- pivot_longer(df, -c(Input, Rtype), names_to="variable", values_to="value")
dfm
#> # A tibble: 24 × 4
#>    Input    Rtype variable     value
#>    <chr>    <chr> <chr>        <int>
#>  1 12-proc. typea Rcost           36
#>  2 12-proc. typea Rsolutions  614425
#>  3 12-proc. typea Btime           40
#>  4 12-proc. typea Bcost           36
#>  5 15-proc. typeb Rcost           51
#>  6 15-proc. typeb Rsolutions  534037
#>  7 15-proc. typeb Btime           50
#>  8 15-proc. typeb Bcost           51
#>  9 18-proc  typec Rcost           62
#> 10 18-proc  typec Rsolutions 1843820
#> # … with 14 more rows

# If you have all 18 factor levels in your data:
ggplot(dfm, aes(x = interaction(Input, Rtype, sep = " "),
                y = value, fill = variable)) +
  geom_bar(stat = "identity") +
  scale_y_log10(labels = scales::dollar,
                name = "log10(cost)") +
  scale_fill_viridis_d(end = 0.8) +
  theme(axis.title.x = element_blank(),
        axis.text.x = element_text(angle = 90, vjust = 0.5))

# If you don't have all 18 factor levels in your data:
all_combinations <- expand_grid(Input = dfm$Input,
                                Rtype = dfm$Rtype) %>%
  distinct()

dfm_expanded <- left_join(all_combinations, dfm) %>%
  replace_na(list("0"))
#> Joining, by = c("Input", "Rtype")

ggplot(dfm_expanded, aes(x = interaction(Input, Rtype, sep = " "),
                         y = value, fill = variable)) +
  geom_bar(stat = "identity") +
  scale_y_log10(labels = scales::dollar,
                name = "log10(cost)") +
  scale_fill_viridis_d(end = 0.8) +
  theme(axis.title.x = element_blank(),
        axis.text.x = element_text(angle = 90, vjust = 0.5))
#> Warning: Removed 12 rows containing missing values (position_stack).

Created on 2022-04-04 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文