使用此代码:
ggplot(Template.2006.2017, aes(x=Disaster_category, y=Total_US_received_from.CERF)) +
ggtitle("MEAN Total Funding Received CERF") +
geom_bar(stat="summary", fun.y = "mean", fill="lightblue") +
coord_flip() + labs(y="US$") + labs(x="")
我创建了此条形图:
显示各种灾难类别的资金。现在,我想删除所有获得零资金资助的类别(其他,虫害,疾病和 missing / NA )。如何在r中完成此操作?
这是我的数据(压缩后):
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), .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),
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), 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)), row.names = c(NA, 150L), class = "data.frame")
答案 0 :(得分:0)
从数据框中删除NA值是否可以解决您的问题?
library(tidyverse)
Template.2006.2017 %>%
filter(!is.na(Total_US_received_from.CERF)) %>%
ggplot(aes(x=Disaster_category, y=Total_US_received_from.CERF)) +
ggtitle("MEAN Total Funding Received CERF") +
geom_bar(stat="summary", fun.y = "mean", fill="lightblue") +
coord_flip() + labs(y="US$") + labs(x="")
答案 1 :(得分:0)
在绘制之前考虑使用ave
来计算组均值,然后将均值大于零的所有行子集作为绘制的基础数据:
# ADD NEW INLINE GROUP MEAN
Template.2006.2017$CERF_categ_mean <- with(Template.2006.2017,
ave(Total_US_received_from.CERF, Disaster_category,
FUN=function(x) mean(x, na.rm=TRUE)))
# SUBSET DATAFRAME
sub_df <- subset(Template.2006.2017, CERF_categ_mean > 0)
# PLOT SUBSETTED DATA
ggplot(sub_df, aes(x=Disaster_category, y=Total_US_received_from.CERF)) +
ggtitle("MEAN Total Funding Received CERF") +
geom_bar(stat="summary", fun.y = "mean", fill="lightblue") +
coord_flip() + labs(y="US$") + labs(x="")