library(ggplot2)
library(viridis)
library(hrbrthemes)
# create a dataset
specie <- c(rep("sorgho" , 3) , rep("poacee" , 3) , rep("banana" , 3) , rep("triticum" , 3) )
condition <- rep(c("normal" , "stress" , "Nitrogen") , 4)
value <- abs(rnorm(12 , 0 , 15))
data <- data.frame(specie,condition,value, stringsAsFactors = FALSE)
data$condition[c(11, 12)] <- "other"
# Graph
ggplot(data, aes(fill = condition, y=value, x=condition)) +
geom_bar(position="dodge", stat="identity") +
scale_fill_viridis(discrete = T, option = "E") +
ggtitle("Studying 4 species..") +
facet_grid(specie~., scales = "free", space = "free") +
theme_ipsum() +
theme(legend.position="none") +
xlab("") +
coord_flip() +
theme(strip.text.x = element_text(angle = 0)) +
theme_classic()
There are 2 rows in the triticum
facet that contain the value other
for the column condition
.
This is done so that 2 separate bars will appear for this value. Instead, only 1 bar appears. How can the former be accomplished?
One option could be to define a helper variable for the plot, say cond2
, in data
with discrete names, but label by condition
using scale_x_discrete
, e.g.:
library(ggplot2)
library(viridis)
specie <- c(rep("sorgho" , 3) , rep("poacee" , 3) , rep("banana" , 3) , rep("triticum" , 3) )
condition <- rep(c("normal" , "stress" , "Nitrogen") , 4)
value <- abs(rnorm(12 , 0 , 15))
data <- data.frame(specie,condition,value, stringsAsFactors = FALSE)
data$condition[c(11, 12)] <- "other"
data$cond2 <- data$condition
data$cond2[c(11, 12)] <- make.unique(data$condition[c(11, 12)])
# Graph
ggplot(data, aes(fill = condition, y=value, x=cond2)) +
geom_bar(position="dodge", stat="identity") +
scale_fill_viridis(discrete = T, option = "E") +
scale_x_discrete(labels=setNames(data$condition, data$cond2)) +
ggtitle("Studying 4 species..") +
facet_grid(specie~., scales = "free", space = "free") +
theme(legend.position="none") +
xlab("") +
coord_flip() +
theme(strip.text.x = element_text(angle = 0)) +
theme_classic()
Created on 2021-05-31 by the reprex package (v2.0.0)