R 中的分组条形图 - 'height' 必须是向量或矩阵
Grouped bar plot in R - 'height' must be a vector or a matrix
我有以下数据,我想制作一个分组条形图,其中显示按位置分组的 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' 必须是向量或矩阵”
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()
我有以下数据,我想制作一个分组条形图,其中显示按位置分组的 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' 必须是向量或矩阵”
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()