youtube video link with explanations for these examples https://youtu.be/k-IN6HBhgq4

Box plots using GGPLOT https://youtu.be/48b4BzxHHH8

library(ggplot2)
library(plyr)
# we will be using chickwts dataset in the examples

head(chickwts)
p <- ggplot(chickwts, aes(x= feed, y = weight)) 
p <- p +   geom_boxplot() 
p <- p + theme_classic()
p <- p + labs(title = "Box Plot") 
p <- p + labs(subtitle ="Basic boxplot showing weight for each feed type")

p

p <- ggplot(chickwts, aes(x= feed,  y = weight)) 
p <- p +   geom_boxplot() 
p <- p + theme_classic()
p <- p + coord_flip()
p <- p + labs(title = "Box Plot") 
p <- p + labs(subtitle ="Using coord_flip to change the orientation of the axis")

p

p <- ggplot(chickwts, aes(x= feed, y = weight, fill = feed)) 
p <- p +   geom_boxplot() 
p <- p + theme_classic()
p <- p + labs(title = "Box Plot") 
p <- p + labs(subtitle ="Using fill command within aes to colour each box with different colours")

p

p <- ggplot(chickwts, aes(x= feed, y = weight, fill = feed)) 
p <- p +   geom_boxplot() 
p <- p + stat_boxplot(geom = 'errorbar', width = 0.2)
p <- p + theme_classic()
p <- p + labs(title = "Box Plot") 
p <- p + labs(subtitle ="Adding the standard error bars in boxplots")

p

p <- ggplot(chickwts, aes(x= feed, y = weight)) 
p <- p +   geom_boxplot(fill ="blue", alpha = 0.5) 
p <- p + theme_classic()
p <- p + labs(title = "Box Plot") 
p <- p + labs(subtitle ="Using fill in geom_boxplot to fill all boxes with a colour")

p

p <- ggplot(chickwts, aes(x= reorder(feed, weight, FUN = median), y = weight, fill = feed)) 
p <- p +  geom_boxplot() 
p <- p + theme_classic()
p <- p + labs(title = "Box Plot") 
p <- p + labs(subtitle ="Reordering the position in ascending order of weight")

p

p <- ggplot(chickwts, aes(x= reorder(feed, weight, FUN = median), y = weight, fill = feed)) 
p <- p +   geom_boxplot(outlier.colour = "red", outlier.shape = 1) 
p <- p + theme_classic()
p <- p + labs(title = "Box Plot") 
p <- p + labs(subtitle ="Specifying the outliers colour and shape")

p

p <- ggplot(chickwts, aes(x= reorder(feed, weight, FUN = median), y = weight, fill = feed)) 
p <- p +  geom_boxplot(outlier.colour = "red", outlier.shape = 1) 
p <- p + geom_point()
p <- p + theme_classic()
p <- p + labs(title = "Box Plot") 
p <- p + labs(subtitle ="showing each data point as well as box plot")

p

p <- ggplot(chickwts, aes(x= reorder(feed, weight, FUN = median), y = weight, fill = feed)) 
p <- p +  geom_boxplot(outlier.colour = "red", outlier.shape = 1) 
p <- p + geom_jitter()
p <- p + theme_classic()
p <- p + labs(title = "Box Plot") 
p <- p + labs(subtitle ="Using geom_jitter to scatter the points and avoid overlapping points")

p

p <- ggplot(chickwts, aes(x= reorder(feed, weight, FUN = median), y = weight, fill = feed)) 
p <- p +  geom_boxplot(outlier.colour = "red", outlier.shape = 1) 
p <- p + geom_jitter(shape = 2, color ="blue", size = 1)
p <- p + theme_classic()
p <- p + labs(title = "Box Plot") 
p <- p + labs(subtitle ="Changing the shape, colour and size for points")

p

p <- ggplot(chickwts, aes(x= reorder(feed, weight, FUN = median), y = weight, fill = feed)) 
p <- p +  geom_boxplot(outlier.shape = NA) 
p <- p + geom_jitter(shape = 2, color ="blue", size = 1)
p <- p + theme_classic()
p <- p + labs(title = "Box Plot") 
p <- p + labs(subtitle ="Hide the outlier points")

p

p <- ggplot(chickwts, aes(x= reorder(feed, weight, FUN = median), y = weight, fill = feed)) 
p <- p +  geom_boxplot() 
#p <- p + geom_jitter(shape = 2, color ="blue", size = 1)
p <- p + theme_classic()
p <- p + labs(title = "Box Plot") 
p <- p + labs(subtitle ="showing the mean value for each group")
p <- p + stat_summary(fun.y = mean, geom="point",colour="darkred", size=2)
## Warning: `fun.y` is deprecated. Use `fun` instead.
p

f <- function(x) {
    s <- boxplot.stats(x)
    data.frame(ymin = s$conf[1], ymax = s$conf[2])
}

p <- ggplot(chickwts, aes(x= feed, y = weight, fill = feed)) 
p <- p + geom_boxplot()
p <- p + stat_summary(aes(fun.data = weight), geom = "linerange", colour = "red", size = 2)
## Warning: Ignoring unknown aesthetics: fun.data
p <- p + stat_summary(fun.y = mean, geom="point",colour="darkred", size=2)
## Warning: `fun.y` is deprecated. Use `fun` instead.
p <- p + theme_classic()
p <- p + labs(title = "Box Plot") 
p <- p + labs(subtitle ="Showing the confidence intervals for each group")

p
## No summary function supplied, defaulting to `mean_se()`

# Now we will use a different dataset for some more charts
# we want to experiment with numerical values on the x axis 
# and create different groups.
library(ggplot2movies)
head(movies)
library(ggplot2movies)
p <- ggplot(movies, aes(y = votes, x = rating, group = rating)) 
p <- p + geom_boxplot() 
p <- p + theme_classic()
p <- p + labs(title = "Box Plot") 
p <- p + labs(subtitle ="We can not see the box plots as the y axis values have large variations
              and the box plots are very small and not visible clearly")
p

p <- ggplot(movies, aes(y = votes, x = rating, group = rating)) 
p <- p + geom_boxplot() 
p <- p + coord_trans(y = "log10") 
p <- p + theme_classic()
p <- p + labs(title = "Box Plot") 
p <- p + labs(subtitle ="We have used the log10 scale for the y axis and we can see the boxplots")
p

We can use library(plyr) The round_any function allows to round to multiple of any number. syntax is round_any(x, accuracy, f = round)

library(plyr)
# let the function round to the nearest rounded value
round_any(2.4, 1)
## [1] 2
# force rounding to the ceiling ( higher side)
round_any(2.4, 1, ceiling)
## [1] 3
# force rounding to the floor (lower side)
round_any(2.4, 1, floor)
## [1] 2
# usage showing the full syntax
round_any(x= 2.8, accuracy = 1, f= ceiling)
## [1] 3
library(plyr)

p <- ggplot(movies, aes(y = votes, x = rating, group = round_any(rating, 1))) 
p <- p + geom_boxplot() 
p <- p + coord_trans(y = "log10") 
p <- p + theme_classic()
p <- p + labs(title = "Box Plot") 
p <- p + labs(subtitle ="Creating a group of rating valuesMovies ratings")
p

p <- ggplot(movies, aes(y = votes, x = rating, group = round_any(rating, 5))) 
p <- p + geom_boxplot() 
p <- p + coord_trans(y = "log10") 
p <- p + theme_classic()
p <- p + labs(title = "Box Plot") 
p <- p + labs(subtitle ="Movies ratings")
p

library(plyr)
p <- ggplot(movies, aes(y = votes, x = rating, group = round_any(rating, 5), fill = factor(round_any(rating, 5))) )
p <- p + geom_boxplot(notch = TRUE)
p <- p + coord_trans(y = "log10") 
p <- p + theme_classic()
p <- p + labs(title = "Box Plot") 
p <- p + labs(subtitle ="Movies ratings")
p