【问题标题】:Grouped bar plot in R - 'height' must be a vector or a matrixR中的分组条形图-“高度”必须是向量或矩阵
【发布时间】:2021-04-28 22:49:06
【问题描述】:

我有以下数据,我想制作一个分组条形图,其中显示按位置分组的 total_deaths_per_million 和 total_tests_per_thousand

 location total_deaths_per_million total_tests_per_thousand
  <chr>                       <dbl>                    <dbl>
1 Albania                      617.                     150.
2 Denmark                      407.                    2818.
3 Germany                      837.                     524.
4 Russia                       578.                     756.

每次我使用 barplot 时,它都会说“'height' 必须是向量或矩阵”

【问题讨论】:

  • 您确实需要提供您的实际代码,而不是对其的描述。还要为您的数据提供dput(),而不是提供无法轻松剪切并粘贴到 R 中的表格。

标签: r bar-chart


【解决方案1】:
  library(tidyverse)
  df <- tribble(~location, ~total_deaths_per_million, ~total_tests_per_thousand,
 "Albania" ,                     617,                     150,
 "Denmark" ,                     407,                    2818,
 "Germany" ,                     837,                     524,
 "Russia"  ,                     578,                     756)

df2 <- df %>% 
  pivot_longer(-location)
barplot(value ~ name + location, data = df2,
        beside = TRUE)

df %>% 
  pivot_longer(-location) %>% 
  ggplot(aes(location, value, fill = name)) +
  geom_col(position = "dodge")

df %>% 
  pivot_longer(-location) %>% 
  ggplot(aes(name, value)) +
  geom_col(position = "dodge") +
  facet_wrap(~location) + coord_flip()

【讨论】:

    猜你喜欢
    • 2018-05-15
    • 2014-06-15
    • 1970-01-01
    • 2015-09-13
    • 2023-04-06
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多