This can be achieved using by adding the geom_jitter option. Library ( grid ) fill <- "#4271AE" lines <- "#1F3552" p10 <- ggplot ( airquality, aes ( x = Month, y = Ozone )) + geom_boxplot ( colour = lines, fill = fill, size = 1 ) + scale_y_continuous ( name = "Mean ozone in\nparts per billion", breaks = seq ( 0, 175, 25 ), limits = c ( 0, 175 )) + scale_x_discrete ( name = "Month" ) + ggtitle ( "Boxplot of mean ozone by month" ) + theme_bw () + theme ( = element_line ( colour = "#d3d3d3" ), = element_blank (), panel.border = element_blank (), panel.background = element_blank (), plot.title = element_text ( size = 14, family = "Tahoma", face = "bold" ), text = element_text ( family = "Tahoma" ), axis.title = element_text ( face = "bold" ), = element_text ( colour = "black", size = 11 ), = element_text ( colour = "black", size = 9 ), axis.line = element_line ( size = 0.5, colour = "black" )) p10Īn extra feature you can add to boxplots is to overlay all of the points for that group on each boxplot in order to get an idea of the sample size of the group. Here is a custom plot where we have modified the axes, background and font. Library ( ggthemes ) library ( grid ) fill <- "#4271AE" line <- "#1F3552" p10 <- ggplot ( airquality, aes ( x = Month, y = Ozone )) + geom_boxplot ( fill = fill, colour = line ) + scale_y_continuous ( name = "Mean ozone in\nparts per billion", breaks = seq ( 0, 175, 25 ), limits = c ( 0, 175 )) + scale_x_discrete ( name = "Month" ) + ggtitle ( "Boxplot of mean ozone by month" ) + theme_economist () + theme ( = element_line ( size = 0.5, colour = "black" ), = element_line ( size = 0.5, colour = "black" ), legend.position = "bottom", legend.direction = "horizontal", legend.box = "horizontal", = unit ( 1, "cm" ), plot.title = element_text ( family = "Tahoma" ), text = element_text ( family = "Tahoma" ), axis.title = element_text ( size = 12 ), legend.text = element_text ( size = 9 ), legend.title = element_text ( face = "bold", size = 9 )) p10Īs before, you can modify your plots a lot as ggplot2 allows many customisations. Below we’ve applied theme_economist(), which approximates graphs in the Economist magazine. There are a wider range of pre-built themes available as part of the ggthemes package (more information on these here). P10 <- ggplot ( airquality, aes ( x = Month, y = Ozone )) + geom_boxplot ( colour = "black", fill = "#56B4E9" ) + scale_y_continuous ( name = "Mean ozone in\nparts per billion", breaks = seq ( 0, 175, 25 ), limits = c ( 0, 175 )) + scale_x_discrete ( name = "Month" ) + ggtitle ( "Boxplot of mean ozone by month" ) + theme ( = element_line ( size = 0.5, colour = "black" ), = element_line ( size = 0.5, colour = "black" ), axis.line = element_line ( size = 1, colour = "black" ), = element_blank (), = element_blank (), panel.border = element_blank (), panel.background = element_blank (), plot.title = element_text ( size = 20, family = "xkcd-Regular" ), text = element_text ( size = 16, family = "xkcd-Regular" ), = element_text ( colour = "black", size = 12 ), = element_text ( colour = "black", size = 12 )) p10
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |