There are a wider range of pre-built themes available as part of the ggthemes package (more information on these here). In order to create this chart, you first need to import the XKCD font, install it on your machine and load it into R using the extrafont package.įill <- c ( "#56B4E9", "#F0E442", "violetred1" ) p6 <- ggplot ( aq_trim, aes ( x = Day, y = Ozone, size = Wind, fill = Month )) + geom_point ( shape = 21 ) + ggtitle ( "Air Quality in New York by Day" ) + labs ( x = "Day of the month", y = "Ozone (ppb)", size = "Wind Speed (mph)", fill = "Months" ) + scale_x_continuous ( breaks = seq ( 1, 31, 5 )) + scale_fill_manual ( values = fill ) + scale_size ( range = c ( 1, 10 )) + theme ( legend.position = "bottom", legend.direction = "horizontal", legend.box = "horizontal", = unit ( 1, "cm" ), axis.line = element_line ( size = 1, colour = "black" ), = element_blank (), = element_blank (), panel.border = element_blank (), panel.background = element_blank (), plot.title = element_text ( family = "xkcd-Regular" ), text = element_text ( family = "xkcd-Regular" ), = element_text ( colour = "black", size = 10 ), = element_text ( colour = "black", size = 10 )) p6 Below is an example of a theme Mauricio was able to create which mimics the visual style of XKCD. ggplot2 allows for a very high degree of customisation, including allowing you to use imported fonts. Of course, you may want to create your own themes as well. P6 <- ggplot ( aq_trim, aes ( x = Day, y = Ozone, size = Wind, fill = Month )) + geom_point ( shape = 21 ) + theme_bw () + theme () + ggtitle ( "Air Quality in New York by Day" ) + labs ( x = "Day of the month", y = "Ozone (ppb)", size = "Wind Speed (mph)", fill = "Months" ) + scale_x_continuous ( breaks = seq ( 1, 31, 5 )) + scale_fill_manual ( values = fill ) + scale_size ( range = c ( 1, 10 )) + theme ( legend.position = "bottom", legend.direction = "horizontal", legend.box = "horizontal", = unit ( 1, "cm" )) p6 The first thing to do is load in the data, as below: The book is also actively maintained (unlike the series on the blog) and contains up-to-date ggplot and tidyverse code, and every purchase really helps us out with keeping up with new content. If you enjoyed this blog post and found it useful, please consider buying our book! It contains chapters detailing how to build and customise all 11 chart types published on the blog, as well as LOWESS charts. In order to reduce the complexity of these data a little, we will only be looking at the final three months in the dataset (July, August and September). We will use R’s airquality dataset in the datasets package. These plots are also called ‘balloon plots’ or ‘bubble plots’. In this tutorial we will demonstrate some of the many options the ggplot2 package has for creating and customising weighted scatterplots. For now, we just need to be aware that ggplots are constructed by specifying the different components that we want to display, based on underlying information in a data frame.This is the fifth tutorial in a series on using ggplot2 I am creating with Mauricio Vargas Sepúlveda. The philosophy of ggplot is much better explained by the package author, Hadley Wickham here. The most common components of a ggplot are This information is usually clarified in a key, or legend, which can also be considered part of this ‘grammar’. Furthermore, the colour, shape or size of points and lines can be used to encode additional information in the plot. The visual way that the data is represented forms another component of the grammar of graphics. Similarly, all plots have data represented between the axes, often as points, lines or bars. For example, all plots require axes, so the x and y axes form one part of the ‘language’ of a plot. The ‘grammar’ of graphics relates to the different components of a plot that function like different parts of linguistic grammar. The ggplot2 package is widely used and valued for its simple, consistent approach to making plots.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |