Indicating a range for the Y axis in a bar chart

HAFZ

For my data the average normally lies between 8,000 and 10,000 and I want to indicate this range on my bar chart below, I want to show to red lines from y=10,000 and y=8,000 and potentially shade the area in between them, if possible. Bar chart attachted

Monthly_accidents2 %>%
  ggplot(aes(x=Month,y=Traffic_Accidents))+
  geom_bar(stat ="identity",fill = "#97B3C6")+
  geom_text(aes(label = Traffic_Accidents), vjust = 0.5, colour = "white")+
  ylim(0,12000)+
  #coord_flip()+
  theme_dark()+
  labs(x=NULL,
       y="Number of traffic accidents",
       title = "                                 Traffic Accidents throughout the year")

Thanks for any possible help in advance.

I tried creating a data set and adding the two lines but it didn't work.

stefan

One option to achieve your desired result would be to use geom_hline to add some horizontal lines and annotate to add a shaded rectangle:

Using some fake example data:

Monthly_accidents2 <- data.frame(
  Month = factor(month.abb, month.abb),
  Traffic_Accidents = 1000 * seq_len(12)
)

library(ggplot2)

base <- ggplot(Monthly_accidents2, aes(x = Month, y = Traffic_Accidents)) +
  geom_col(fill = "#97B3C6") +
  geom_text(aes(label = Traffic_Accidents), vjust = 0.5, colour = "white") +
  ylim(0, 12000) +
  theme_dark() +
  labs(
    x = NULL,
    y = "Number of traffic accidents",
    title = "Traffic Accidents throughout the year"
  ) +
  theme(plot.title = element_text(hjust = .5))

base +
  geom_hline(yintercept = c(8000, 10000), color = "red") +
  annotate(geom = "rect", ymin = 8000, ymax = 10000, xmin = -Inf, xmax = Inf, fill = "red", alpha = .2)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to make a bar chart on range of values on x-axis and count on the range on y-axis in python?

How to show unit on bar chart range axis

Is it possible to change color text or background of label Y-Axis of Range bar chart

Zingchart bar chart starting at y axis -50

Y axis in single stacked bar chart

weird y-axis dodged bar chart

Horizontal bar chart y axis alignment

Stacked bar chart swap x-axis and y-axis

Plot bar chart with specific range of x axis in MATLAB

R: ggplot stacked bar chart with counts on y axis but percentage as label

Python - Floating Bar Chart with y axis set at 0

d3 multiple y axis for multiple bar chart

Get unique max value for each y-axis in a bar chart

Stacked bar chart bars dimension is not coralated to Y axis

How to adjust the Y Axis maximum value on a bar chart in LibreOffice Calc

Make the Y-axis scale exponentially in bar chart matlab

Android : Bar chart changing y axis scale with achartengine

D3.js bar chart Y-axis issue

dimple js straight line y-axis over bar chart

Display y axis value horizontal line drawn In bar chart

ggplot2 bar chart sign on y-axis

Unable to set y axis limits in ggplotly bar chart in R

In Highcharts, how to reverse y-axis under "Bar" chart type?

How to arrange y axis in bar chart using ggplot2

Horizontal Stacked Bar Chart with Two Y Axis Issues

Specify Y-axis minimum, maximum and grid lines for Bar chart

How to remove y-axis from grouped bar chart

c3 bar chart Y axis value with fixed value

Is it possible to make a table with y axis labels of Highcharts bar chart?