I would like to plot a number of symmetric bars like these two, in which the width of the bar corresponds to the relative abundance of the variable through time. I could not find anything similar in R; any help is appreciated.
1 Answers
2
votes
Are you looking for a violin plot?
As per your comment, the violin plot is not what you are after. There are two approximate solutions, neither of them ideal but they get you a bit further:
library(dplyr)
library(tibble)
library(ggplot2)
set.seed(123)
data <- tibble(
Date = seq.Date(from = as.Date("2020/01/01"), length = 50, by = "day"),
Value = runif(50, min = 0, max = 10)
)
data <- data %>%
mutate(Value_plus = Value,
Value_min = -Value)
p <- ggplot(data = data, aes(fill = "red")) +
geom_step(aes(x = Date, y = Value_plus)) +
geom_step(aes(x = Date, y = Value_min))
p
p <- ggplot(data = data, ) +
geom_ribbon(aes(x = Date, ymin = Value_min, ymax = Value_plus))
p
The first plot has the steps that you suggest in your example but a fill for geom_step appears non-trivial. The second plot, using geom_ribbon gives you a fill but not the steps. There are several examples of solutions (e.g. here) on how to get to a filled step plot.
Using geom_step:



y2 = c(x, rev(x))andx2 = c(y, -rev(y))(flipping x and y to make it vertical). Might have to offset the black and white bars a bit though. - teunbrand