r - 从条形图中删除类别

问题描述 投票:1回答:2

使用此代码:

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="")

我创建了这个条形图:

enter image description here

显示各种灾难类别的资金。现在,我想删除所有已获得零资金的类别(其他,昆虫感染,疾病和缺失/ 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")
r ggplot2 plot
2个回答
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="")

Plot Output


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="")

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.