monthly boxplot of two stations in one graph

hi everyone, am new on ggplot2, please i want to plot monthly variability boxplot of two stations namely "Island" and "Inland" in same graph, which means i expect a group and sub-group of the dataframe, the group is the "Month"(X-Axis) which consists of "Jan" to "Dec", the sub-group comprises of stations("Island" and "Inland"). a similar expected boxplot, is attached herein

Zoo <- read_excel("C:/Users/NJOKU/Desktop/Zoo.xlsx")

View(Zoo)
group_by(Zoo,Station)

A tibble: 750 x 4

Groups: Station [2]

Rainfall Month Station Year

1 15 Jan Island 1984
2 53.7 Jan Island 1985
3 12.9 Jan Island 1986
4 4.1 Jan Island 1987
5 76.9 Jan Island 1988
6 0 Jan Island 1989
7 0 Jan Island 1990
8 15.9 Jan Island 1991
9 0 Jan Island 1992
10 0 Jan Island 1993

... with 740 more rows

group_by(Zoo, Month)

A tibble: 750 x 4

Groups: Month [12]

Rainfall Month Station Year

1 15 Jan Island 1984
2 53.7 Jan Island 1985
3 12.9 Jan Island 1986
4 4.1 Jan Island 1987
5 76.9 Jan Island 1988
6 0 Jan Island 1989
7 0 Jan Island 1990
8 15.9 Jan Island 1991
9 0 Jan Island 1992
10 0 Jan Island 1993

... with 740 more rows

My Y-Axis should be "Rainfall(mm)
below are boxplots similar boxplots i created but i want the two stations in one graph in this present workboxplotIkeja roof_boxplot

Use the fill aesthetic, see this example:

library(ggplot2)

zoo <- data.frame(stringsAsFactors=FALSE,
    Rainfall = c(15, 53.7, 12.9, 4.1, 76.9, 0, 0, 15.9, 0, 0, 
                 12, 54.3, 11, 3.2, 67.4, 1, 2, 14, 0, 1),
       Month = c("Jan", "Jan", "Jan", "Jan", "Jan", "Feb", "Feb", "Feb", "Feb", "Feb",
                 "Jan", "Jan", "Jan", "Jan", "Jan", "Feb", "Feb", "Feb" , "Feb", "Feb"),
     Station = c("Island", "Island", "Island", "Island", "Island", "Island",
                 "Island", "Island", "Island", "Island", "Inland", "Inland", "Inland",
                 "Inland", "Inland", "Inland", "Inland", "Inland", "Inland", "Inland"),
        Year = c(1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 
                 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993)
)

ggplot(zoo, aes(x = Month, y = Rainfall, fill = Station)) +
    geom_boxplot()

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

Thank you for helping out, but this is not exactly how i want it, i don't want outliers and i need the whiskers as displayed on my plot, maybe you can drop your e-mail so i can send you my data, the code is very important to me because i want to understand every detail of the argument, my challenge starts from the defining the data frame.

Sorry I don't do email support, you can get help here if you provide the data on a copy/paste friendly format (or at least a link to it), as explained in the link I gave you before.

ok, am studying the Reprex link, i will do it now.

i think i have made so much progress from what i have learnt from you, this is what i obtained and it is 95% what i want, my only challenge is how the bars of the boxplot looks, i think am missing something, below is my code and the output boxplot.

library(ggplot2)
Zoo$Month<-factor(Zoo$Month,levels = c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"))
Zoo$Station<-factor(Zoo$Station,levels = c("Island","Inland"))
ggplot(data = Zoo,aes(x=Month, y=Rainfall,fill= Station))+stat_boxplot(geom = "errorbar",width=0.4)+geom_boxplot(outlier.colour = NA)+xlab("Month")+ylab("Rainfall(mm)")+labs(title = "Zoo weather Stations")+theme(plot.title = element_text(hjust = 0.5))+scale_y_continuous(breaks =seq(0,700,by=100),limits = c(0,700))
Warning messages:
1: Removed 1 rows containing non-finite values (stat_boxplot).
2: Removed 1 rows containing non-finite values (stat_boxplot). twostationsboxplot

Thanks guys, i got my solution, this is the code below:

ggplot(data = Zoo,aes(x = Month, y = Rainfall, fill = Station)) + 
    stat_boxplot(geom = "errorbar") +
    geom_boxplot(outlier.colour = NA) +
    xlab("Month") + 
    ylab("Rainfall(mm)") + 
    labs(title = "Zoo weather Stations") + 
    theme(plot.title = element_text(hjust = 0.5)) + 
    scale_y_continuous(breaks = seq(0, 700, by = 100), limits = c(0, 700))

finaldoubleplot

2 Likes

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.