0
votes

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.

t

1
Can you provide some example data to work with? This isn't really a programming question in its current form. I would also think about whether this is a good visualization - I find it rather confusing - perhaps there is a better way? - neilfws
Wouldn't this be solved by defining the polygon you want? If you have the x/y coordinates of half of the the bar, you could do y2 = c(x, rev(x)) and x2 = 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

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:

enter image description here

Using geom_ribbon: enter image description here