I have a dataset that I would like use to create several graphs via ggplot. In particular, I'm interested in creating some graphs that will display proportions of one variable at a time by groups of interest.
For example, if I had the following data (taken from
What would be the smartest way to create said graph via ggplot? My actual dataset has many more variables, so I'm trying to avoid creating summarised datasets with the percentage calculation for each individual variable worked out, if that makes sense.
From there I'd like to create a combo bar graph to display the raw numbers too like the following image, but I think if I can work out displaying proportions I'll be ok to figure out the rest from other stackoverflow questions like
CodePudding user response:
Is this what you are looking for :
Code:
ggplot(data, aes(x=year, y=value))
geom_col(position = "stack", fill = "darkblue")
stat_summary(fun=mean, geom="line", colour= "orange", size = 2)
facet_wrap(~condition)
CodePudding user response:
Here is a solution.
Given that there are three groups, first compute proportions by year and condition, creating a summary data set. Then plot it, with a secondary axis scaled to the maximum value.
set.seed(2022)
specie <- c(rep("sorgho" , 6) , rep("poacee" , 6) , rep("banana" , 6) , rep("triticum" , 6) )
condition <- rep(c("normal" , "stress" , "Nitrogen") , 8)
year <- sample(2010:2015, 24, T)
value <- abs(rnorm(24 , 0 , 15))
data <- data.frame(specie,condition,year,value)
suppressPackageStartupMessages({
library(ggplot2)
library(dplyr)
})
smrydata <- data %>%
group_by(year) %>%
mutate(total = sum(value)) %>%
group_by(condition, year) %>%
summarise(value = sum(value),
prop = value/sum(total),
.groups = "drop")
Max <- max(smrydata$value)
ggplot(smrydata, aes(year, value))
geom_col(fill = "steelblue")
geom_line(aes(y = prop * Max), colour = "orange", size = 2)
scale_y_continuous(sec.axis = sec_axis( ~ ./Max, labels = scales::percent))
facet_wrap(~ condition)
theme_bw()
Created on 2022-09-12 by the reprex package (v2.0.1)