【问题标题】:Multiple Stacked Bar Charts with ggplot()带有 ggplot() 的多个堆积条形图
【发布时间】:2018-09-22 21:53:00
【问题描述】:

我是 R 的新手,我一直坚持在 ggplot2 中创建以下条形图:

这是我目前的代码:

#Read in data
parameter_results<- readRDS("param_results_2014.RDS")

#list of parameter names
parameters <- sort(readRDS("parameters.RDS"))

bar_plot <- function(parameter) {
  parameter_df <- parameter_results %>%
    select(results = parameter) %>%  #keep only column for the parameter you want to plot
    filter(results != "Not Applicable") %>% 
    count(results) %>%    
    mutate(prop = prop.table(n), perc = paste0(round(prop * 100),"%"))
  color_code <- c("Attaining" = "#99FF99","Non Attaining" =  "#FF9999", "Insufficient Information" =  "#FFFF99")

  values <- vector(mode = "numeric", length = nrow(parameter_df))
  labs <- vector(mode = "character", length = nrow(parameter_df))
  colors <- vector(mode = "character", length = nrow(parameter_df))
  for (i in seq_along(1:nrow(parameter_df))) {
    values[[i]] <- parameter_df$prop[[i]] * 100
    labs[[i]] <- parameter_df$perc[i]
    colors[[i]] <- color_code[[parameter_df$results[[i]]]]
  }

  stacked_bar<-ggplot(parameter_df,aes(x=parameter,y=n,fill = fct_inorder(results)))+
    geom_bar(stat = "identity", width = 0.5,color="black") +
    blank_theme + theme(legend.title=element_blank()) +
    ggtitle("Figure ES-2: Statewide Designated Use Assessment Results, 2014") + 
    xlab("Designated Uses")+
    ylab("Number of Assessment Units")+
    theme(plot.title = element_text(hjust = 0.5,vjust=10))   +
    scale_fill_manual(values = c("Attaining" = "#99FF99","Non Attaining" = "#FF9999","Insufficient Information" = "#FFFF99"))      
}

bar_plot()
bar_ALG <-bar_plot('ALG')

我的数据集如下所示:

 A tibble: 958 x 89
   WMA   Waterbody  Name      `Biological (Caus~ `Biological Trout~ DO     `DO Trout` Temperature  `Temperature Tr~ pH    
   <chr> <chr>      <chr>     <chr>              <chr>              <chr>  <chr>      <chr>        <chr>            <chr> 
 1 15    020403020~ Absecon ~ Attaining          Not Applicable     Attai~ Not Appli~ Attaining    Not Applicable   Attai~
 2 15    020403020~ Absecon ~ Insufficient Info~ Not Applicable     Non A~ Not Appli~ Attaining    Not Applicable   Insuf~
 3 15    020403020~ Absecon ~ Attaining          Not Applicable     Insuf~ Not Appli~ Insufficien~ Not Applicable   Non A~
 4 15    020403020~ Absecon ~ Attaining          Not Applicable     Attai~ Not Appli~ Attaining    Not Applicable   Attai~
 5 14    020403011~ Albertso~ Non Attaining      Not Applicable     Attai~ Not Appli~ Attaining    Not Applicable   Non A~
 6 11    020401052~ Alexauke~ Attaining          Attaining          Insuf~ Attaining  Insufficien~ Non Attaining    Non A~
 7 11    020401052~ Alexauke~ Attaining          Attaining          Insuf~ Attaining  Insufficien~ Non Attaining    Non A~
 8 17    020402060~ Alloway ~ Non Attaining      Not Applicable     Attai~ Not Appli~ Attaining    Not Applicable   Attai~
 9 17    020402060~ Alloway ~ Insufficient Info~ Not Applicable     Attai~ Not Appli~ Attaining    Not Applicable   Insuf~
10 17    020402060~ Alloway ~ Insufficient Info~ Not Applicable     Insuf~ Not Appli~ Insufficien~ Not Applicable   Insuf~

参数_df:

parameter_df
## # A tibble: 2 x 4
##                    results     n      prop  perc
##                      <chr> <int>     <dbl> <chr>
## 1                Attaining   454 0.5443645   54%
## 2 Insufficient Information   380 0.4556355   46%

每个参数都有自己的列……数据表的每一行都包含每个参数给定位置的评估值。 我的问题是我需要对数据集或函数做些什么才能使每个参数像上图一样绘制?

这是我得到的情节:

【问题讨论】:

  • @Parfait 是的,我希望我的情节看起来就像我附加的那个。但是我只能用我拥有的函数绘制一个参数而且我不明白如何制作这样我就可以像我想要的图表一样绘制多个参数
  • @shosaco 我试过了,但它似乎不起作用......
  • @Parfait 我附上了我得到的情节。
  • @Parfait 抱歉忘记了最后一行代码。但它是我上面的代码。

标签: r ggplot2 bar-chart


【解决方案1】:

避免跨参数迭代运行图形,而是在整个数据帧上运行,parameter_results。但是,首先考虑使用tidyr::gather 和dplyr::group_by 转换数据来计算类别选项卡:

library(dplyr)
library(tidyr)
library(ggplot2)

# RESHAPE WIDE TO LONG
rdf <- parameter_results %>%
  gather(value = colnames(parameter_results)) %>%
  setNames(c("parameter", "results"))

# GROUP BY PARAMETER CALCULATIONS
graph_df <- rdf %>%
  group_by(parameter) %>%
  filter(results != "Not Applicable") %>% 
  count(results) %>%    
  mutate(prop = prop.table(n), 
         perc = paste0(round(prop * 100),"%"))

color_code <- c("Attaining"="#99FF99", "Non Attaining"="#FF9999", 
                "Insufficient Information"="#FFFF99")

# GRAPH ALL PARAMETERS TOGETHER AT ONCE
ggplot(graph_df, aes(x=parameter, y=n, fill = results)) +
  geom_bar(stat = "identity", width = 0.5,color="black") +
  theme(legend.title=element_blank()) +
  ggtitle("Figure ES-2: Statewide Designated Use Assessment Results, 2014") + 
  xlab("Designated Uses")+
  ylab("Number of Assessment Units") +
  theme(legend.position="bottom", plot.title = element_text(hjust=0.5, vjust=10)) +
  scale_fill_manual(values = color_code) 

输入 (使用200的随机数据,假设parameters_results是类似的结构)

categ <- c("Attaining", "Insufficient Information", "Non Attaining", "Not Applicable")

set.seed(555)
parameter_results <- data.frame(
  Acquatic_Life_Gen = sample(categ, 200, replace=TRUE),
  Acquatic_Life_Trout = sample(categ, 200, replace=TRUE),
  Recreation = sample(categ, 200, replace=TRUE),
  Water_Supply = sample(categ, 200, replace=TRUE),
  Shellfish_Harvest = sample(categ, 200, replace=TRUE),
  Fish_Consumption = sample(categ, 200, replace=TRUE)
)

输出

【讨论】:

  • 感谢工作!但是,我没有收到这个新错误:计数错误(。,结果):找不到对象'结果'......以及.. ggplot中的错误(graph_df,aes(x =参数,y = n,填充) = 结果)):找不到对象“graph_df”@Parfait
  • 我提供了 parameter_results 的更详细视图。见编辑。我对你最后的评论感到很困惑。
  • 请检查graph_df是否正确生成?是否有列、parameter、results、n、prop、perc?错误说它不存在。如果没有,请检查控制台是否有错误。还请删除此线程中以前不需要的 cmets,以免拖太久。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多