Reputation: 555
I have a chart which has an error bar on it:
However, when I put the chart inside a plotly wrapper, the error bar sizing gets messed up, as shown below:
Does anyone have a solution for keeping the error bar width the same size as the bar, as shown in plot 1, but while keeping the plot rendering with plotly?
library(tidyverse)
library(plotly)
dat <- data.frame(peeps= c("Bill", "Bob", "Becky"),
vals = c(10, 15, 12),
goals = c(8, 13, 10),
grp = c("Bears", "Bears", "Mongoose") %>% as.factor)
p1 <- dat %>%
ggplot(aes(x = peeps, y = vals, fill = grp)) +
geom_bar(stat = "identity") +
geom_errorbar(data = dat,
aes(ymin = goals, ymax = goals),
color = "blue",
size = 1,
linetype = 1) +
scale_y_continuous(expand = c(0, 0)) +
coord_flip()
p1
ggplotly(p1) %>%
layout(legend = list(orientation = "h",
xanchor = "center",
y = -0.15,
x = 0.5))
Upvotes: 0
Views: 357
Reputation: 4140
Using geom_segment()
instead of geom_errorbar()
is a work-around for this problem.
dat <- data.frame(peeps= c("Bill", "Bob", "Becky") %>% as.factor,
vals = c(10, 15, 12),
goals = c(8, 13, 10),
grp = c("Bears", "Bears", "Mongoose"),
rowid = 1:3)
p1 <- ggplot(data = dat, aes(x = peeps, y = vals, fill = grp, order = rowid)) +
geom_col() +
geom_segment(aes(
x = as.numeric(peeps)-0.45,
xend = as.numeric(peeps)+0.45,
y = goals, yend = goals),
color = "blue",
size = 1) +
scale_y_continuous(expand = c(0, 0)) +
coord_flip()
ggplotly(p1) %>%
layout(legend = list(orientation = "h",
xanchor = "center",
y = -0.15,
x = 0.5))
Upvotes: 1