0

I am having issues. I created a bargraph using ggplot2 to plot rainfall. But I cannot get the right scale on the y axis and neither can I add the mean over the years. All the missing values were removed from the data set. The bars are correct though, but the y scale are showing 1 and 2. I tried to change "summary" for "identity", but it does not work. I have tried to manually change the y axis values, but it does not work. Please help

enter image description here

str(dat)
dat$Year <- as.character(dat$Year)
dat$Month <- factor(dat$Month)
dat$Day <- factor(dat$Day)
dat$Rainfall <- as.numeric(dat$Rainfall)
ggplot(data=dat, aes(x=Year, y=Rainfall)) +
geom_bar(stat="summary", na.rm=TRUE,width= 0.5, position=position_dodge(width=0.8), fill="orange") +xlab("Rainfall between 1998 - 2023") +
scale_y_continuous(name="Rainfall (mm)",)+
geom_hline(yintercept = mean(dat$Rainfall), linetype ="dashed", linewidth = 1)+
ggtitle("Rainfall at Coonamble Airport between 1998 and 2023")
A. Q.
  • 1
  • 1
  • It's hard to see what the problem is without a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) @A.Q.; are you able to share your data? Please edit your post with the output of the command `dput(dat)` (or `dput(head(dat))` if the dataframe is very large). Otherwise, are you able to reproduce the problem using a built-in dataset such as `mtcars` or `iris` (re http://www.sthda.com/english/wiki/r-built-in-data-sets)? Please see https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example to help make your question 'answerable' – jared_mamrot Aug 20 '23 at 23:03

1 Answers1

0

I used the data within the nycflights13 R package as this probably is one of the closer examples to make this plot. Except for rainfall and years, this plot is months and temperature.

library(ggplot2); library(tidyverse); library(nycflights13)

dat <- nycflights13::weather

dat

ggplot(dat, aes(x = month, y = temp)) + 
  geom_bar(stat = "identity", na.rm = TRUE, width = 0.5, 
           position = position_dodge(width = 0.8), fill = "orange") +
  scale_y_continuous(limits = c(0,100)) + 
  geom_hline(yintercept = 55.26, linetype = "dashed", color = "black", label = "Mean") +
  annotate("text", x = 10.5, y = 58.5, label = "Mean", size = 6) + 
  labs(title = "Monthly Temperatures During Flight Departures in New York", y = "Temperature (F)", 
       x = "Month") +
  theme_bw() 

If possible try to work on the scaling and measurements in the early stages of the code. As you progress work on configuring titles and names on the axis (See Healy 2019 Data Visualization p.56 - great reference for anything ggplot2 related too Data Visualization Online.

I just inputted the value from summary(dat2$temp) in the geom_hline code for the mean.
Otherwise, the other way could be to create a new variable in the data frame that takes the mean of the rainfall. Another change could also be converting year back to a numeric variable rather than a character vector. Using the dat$Year <- as.numeric(dat$Year)