我正在尝试使用ggplot2创建带有三个y变量(Total_us_received
,Total_us_required
,Total_us_received_from.CERF
)的条形图。所有三个y变量均以相同的比例(美元)衡量。
到目前为止,我已经使用以下代码创建了条形图,其中Total_us_received作为y变量,而Disaster_category作为x变量:
ggplot(Template.2006.2017.text,
aes(Disaster_category, y=Total_US_received)) +
geom_bar(stat ="identity", fill="lightblue") +
coord_flip()
但是,我为将其他两个y变量包括到图中所做的每一次尝试都失败了。如何将其他两个变量包含到图中?
一个后续问题:我可以让图显示没有NA:s而不是总和的x变量(Disaster_subtype)的每个类别的平均值吗?
这是我在dput中的数据(压缩版):
structure(list(Disaster_category = structure(c(1L, 15L, 17L,
15L, 5L, 8L, 13L, 8L, 2L, 8L, 2L, 3L, 8L, 2L, 8L, 2L, 10L, 5L,
7L, 8L, 15L, 2L, 8L, 2L, 15L, 15L, 8L, 15L, 2L, 17L, 2L, 7L,
2L, 8L, 2L, 3L, 2L, 8L, 8L, 2L, 8L, 17L, 2L, 3L, 8L, 8L, 2L,
8L, 8L, 8L, 2L, 8L, 3L, 2L, 3L, 2L, 8L, 2L, 3L, 8L, 2L, 8L, 2L,
15L, 5L, 8L, 13L, 8L, 15L, 2L, 8L, 2L, 3L, 2L, 3L, 15L, 8L, 3L,
2L, 3L, 8L, 2L, 3L, 2L, 8L, 2L, 8L, 15L, 2L, 8L, 8L, 5L, 2L,
8L, 2L, 3L, 2L, 17L, 2L, 17L, 2L, 4L, 5L, 8L, 8L, 2L, 8L, 15L,
2L, 15L, 15L, 7L, 2L, 8L, 2L, 15L, 15L, 7L, 8L, 17L, 2L, 15L,
8L, 2L, 17L, 2L, 3L, 8L, 2L, 5L, 2L, 8L, 2L, 8L, 8L, 15L, 2L,
8L, 2L, 15L, 8L, 2L, 15L, 8L, 7L, 8L, 15L, 2L, 8L, 8L, 7L, 13L,
8L, 2L, 8L, 2L, 8L, 8L, 3L, 2L, 13L, 2L, 3L, 8L, 2L, 15L, 15L,
8L, 15L, 2L, 5L, 3L, 3L, 8L, 3L, 2L, 8L, 8L, 3L, 2L, 8L, 2L,
15L, 2L, 17L, 2L, 5L, 2L, 8L, 2L, 15L, 2L, 3L, 8L, 8L, 2L, 8L,
8L, 2L, 3L), .Label = c("", " ", "Disease", "Disease related disaster",
"Drought", "Drought & storm", "Extreme temperature / fire", "Flood",
"Flood & drought", "Insect infestation", "Insect infestation & drought",
"Landslide & flood", "Landslide / mudslide", "Other", "Storm",
"Storm & flood", "Winter"), class = "factor"), Total_US_received_from.CERF = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 678307.8333,
678307.8333, 678307.8333, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1110469.5, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 1905355, NA, NA, NA, NA, NA, NA, NA, NA, NA, 2493246,
2493246, 2493246, 2493246, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 333333.3333, 333333.3333, 333333.3333, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 9365420,
NA, NA, 14321419, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA),
Total_US_received = c(NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, 15507224.5, 15507224.5, 15507224.5,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, 333333.3333, 333333.3333,
333333.3333, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA), Total_US_required = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 20502064.83, 20502064.83, 20502064.83, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 3070192, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 49955895.25, 49955895.25, 49955895.25, 49955895.25,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 333333.3333,
333333.3333, 333333.3333, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), row.names = c(NA,
200L), class = "data.frame")
答案 0 :(得分:0)
您可以将数据转换为长格式,然后绘制它们:
library(tidyr)
library(ggplot2)
my_data %>%
gather(Total_US_category, Total_US, Total_US_received, Total_US_required, Total_US_received_from.CERF) %>%
ggplot(aes(Disaster_category, y = Total_US, fill = Total_US_category)) +
geom_col(position = position_dodge()) +
coord_flip()
如果要绘制每次灾难的平均值,则可以首先使用dplyr
汇总数据:
library(dplyr)
my_data_sum <- my_data %>%
gather(Total_US_category, Total_US, Total_US_received, Total_US_required, Total_US_received_from.CERF) %>%
group_by(Disaster_category, Total_US_category) %>%
summarize(Total_US_mean = mean(Total_US, na.rm = T))
my_data_sum
# A tibble: 33 x 3
# Groups: Disaster_category [?]
# Disaster_category Total_US_category Total_US_mean
# <fct> <chr> <dbl>
# 1 "" Total_US_received NaN
# 2 "" Total_US_received_from.CERF NaN
# 3 "" Total_US_required NaN
# 4 " " Total_US_received NaN
# 5 " " Total_US_received_from.CERF NaN
# 6 " " Total_US_required NaN
# 7 Disease Total_US_received NaN
# 8 Disease Total_US_received_from.CERF NaN
# 9 Disease Total_US_required NaN
# 10 Disease related disaster Total_US_received NaN
# ... with 23 more rows
然后绘制数据:
ggplot(my_data_sum, aes(Disaster_category, y = Total_US_mean, fill = Total_US_category)) +
geom_col(position = position_dodge()) +
coord_flip()