Reputation: 591
I have some plots that I'd like to make sure there is three columns represented on the chart for each quadrat. Is there a way to make a small line appear on the columns that have an average of 0?
# plot average for each ellenberg category for each quadrat
i = 1
for (df in ellenCatTab){
plotName <- names(ellenCatTab)[i]
i = i + 1
col_means <- colMeans(df[,5:31], na.rm=TRUE)
col_meansdf <- stack(col_means)
col_meansdf$Quadrat <- do.call(rbind, quadList)
col_meansdf$Quadrat <- as.factor(col_meansdf$Quadrat)
col_meansdf$Year <- do.call(rbind, yearList)
col_meansdf$Year <- as.factor(col_meansdf$Year)
col_meansdf <- col_meansdf[c('Quadrat', 'Year', 'values')]
p <- ggplot(col_meansdf, aes(x=Quadrat, y=values, fill=Year)) +
ggtitle(plotName) +
labs(x='Quadrat', y='Average Ellenberg Score') +
geom_col(width=0.5, position=position_dodge(0.5), drop=FALSE) +
scale_fill_brewer(palette = 'YlGnBu') +
theme_classic()
plot(p)
ggsave(plot=p,
file=paste0(plotName,'.png'),
limitsize=FALSE,
width=150, height=100, units="mm")
}
structure(list(Quadrat = structure(c(1L, 1L, 1L, 2L, 2L, 2L,
3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L,
8L, 8L, 9L, 9L, 9L), levels = c("Q1", "Q2", "Q3", "Q4", "Q5",
"Q6", "Q7", "Q8", "Q9"), class = "factor"), Year = structure(c(1L,
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L,
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), levels = c("2019", "2020",
"2022"), class = "factor"), values = c(0.0588235294117647, 0.105263157894737,
0.0625, 0.0714285714285714, 0.0666666666666667, 0.0714285714285714,
0.222222222222222, 0.12, 0.12, 0.0625, 0.0769230769230769, 0.0625,
0.1, 0.1, NaN, 0.0625, 0, 0, 0.142857142857143, 0, 0.142857142857143,
0.0588235294117647, 0.1, 0.0588235294117647, 0, 0.0909090909090909,
0)), row.names = c(NA, -27L), class = "data.frame")
Upvotes: 0
Views: 169
Reputation: 78917
This one is a hacky approach:
We could manipulate 0 values and change them to 0.001, and we also should replace NAs with 0.001:
library(tidyverse)
df %>%
mutate(values = ifelse(values == 0, 0.001, values),
values = replace_na(values, 0.001)) %>%
ggplot(aes(x=Quadrat, y=values, fill=Year)) +
labs(x='Quadrat', y='Average Ellenberg Score') +
geom_col(width=0.5, position=position_dodge(0.5), drop=FALSE) +
scale_fill_brewer(palette = 'YlGnBu') +
theme_classic()
Upvotes: 2