I want to use echarts4r
to plot a barplot where values above a cutoff are colored green and below red and the bars start at that value. If the cutoff is 0, we can use the answer provided here, for other values (eg 1 in the example below) this does not work so well as the bar always starts at zero. Is there a way I can make the bar start at other values?
See MWE below:
library(echarts4r)
set.seed(1)
df <- data.frame(
x = 1:10,
y = 1 + cumsum(rnorm(10, 0, 0.1))
)
df %>%
e_charts(x) %>%
e_bar(y) %>%
e_visual_map(
type = "piecewise",
pieces = list(
list(
gt = 1,
color = "green"
),
list(
lte = 1,
color = "red"
)
)
)
Using ggplot2
I would do it like so
library(ggplot2)
CUTOFF <- 1
df$color <- ifelse(df$y > CUTOFF, "green", "red")
ggplot(df, aes(xmin = x - 0.5, xmax = x + 0.5,
ymin = CUTOFF, ymax = y, fill = I(color))) +
geom_rect()