我是R的新手,我一直坚持在ggplot2中创建以下条形图:
这是我到目前为止的代码:
next_time += delay
我的数据集如下所示:
#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')
parameter_df:
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~
每个参数都有自己的列...数据表的每一行都包含每个参数的给定位置的评估值。 我的问题是我需要对数据集或函数做什么才能让每个参数都像上图那样绘制?
这是我得到的情节:
答案 0 :(得分: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)
)
<强>输出强>