R: ggplot2 & Plotly: Recreating 'reference bands' on bar graphs from Tableau in R -
i trying recreate functionality use daily in tableau r (ggplot2 , plotly). need able create reference bands , lines similar image below. i've figured out how create reference lines geom_errorbar(). can't seem find solution 'reference band'.
if solution isn't possible in ggplot2 or plotly open package, need somethign static rmarkdown reports , dynamic html widgets dashboard.
below have sample code, add reference bands of 'high' , 'low' bar graph each person.
library(ggplot2) #create data name <- c("rick","carl","daryl","glenn") pos <- c("m","m","d","d") load <- c(100,110,90,130) high <- c(150,160,130,140) low <- c(130,145,120,130) data <- data.frame(name,pos,load,high,low) rm(name,pos,load,high,low) #create plot ggplot(data = data, aes(x = name, y = load)) + geom_bar(stat ="identity", width=.4)
could guidance appreciated. thank you!
geom_rect()
better choise geom_errorbar()
because can reproduce same image posted. take @ both rect , errorbar documentarion.
the following example used in markdown:
library(dplyr) library(ggplot2) delta <- 0.5 data <- mtcars %>% group_by(cyl, vs) %>% summarise(xmin = first(cyl) - 1, xmax = first(cyl) + 1, wt = mean(wt), ymin = wt - delta, ymax = wt + delta) ggplot(data = data, aes(x = cyl, y = wt)) + geom_rect(aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax), fill = "indianred", alpha = 0.4) + # adds reference band layer before geom_bar(stat = "identity", fill = "darkblue", width = 1) + # bar layer facet_wrap(~vs) + theme_classic()
if wish 1 reference band have use same ymax
, ymin
parameters observations.
you still need more effort in html version, because plotly::ggplotly()
messing up.
Comments
Post a Comment