Then, the colors to be used are defined in the function scale_color_manual. The white background is created by specifying the theme as a black and white theme ( theme_bw()) while the color of the dots is changed by specifying that the color should be applied by Species ( color = GenreRedux). The advantage of ggplot2 is that is really easy to modify the plot by adding new layers and to change the basic outlook by modifying the theme which is what we will do in the code below. As we want a scatter plot with points, we add the geom_point() function without any arguments (as we do not want to specify the size, color, and shape of the points just yet). Then, we need to define the type of plot that we want. The aes function takes the axes as the arguments (in the current case). The aesthetics are defined within the ggplot function as the arguments of aes. This function takes the data set as its first argument and then requires aesthetics. The function call for plotting in is simply ggplot. When creating scatter plots with the ggplot2 package, we use the ggplot function, then we define the data, and then we specify the type of plot using a geom (in this case a geom_point). Scatter plots are used when the graph is set up to display the relationship between two numeric variables. The first, and simplest graph, is a so-called scatter or dot plot. GenreRedux collapses the existing genres into five main categories ( Conversational, Religious, Legal, Fiction, and NonFiction) while DateRedux collapses the dates when the texts were composed into five main periods (1150-1499, 1500-1599, 1600-1699, 1700-1799, and 1800-1913). We also add two more variables to the data called GenreRedux and DateRedux. The data set is based on the Penn Parsed Corpora of Historical English (PPC) and it contains the date when a text was written ( Date), the genre of the text ( Genre), the name of the text ( Text), the relative frequency of prepositions in the text ( Prepositions), and the region in which the text was written ( Region). The data set is called lmmdata but we will change the name to pdat for this tutorial. Once you have installed R and RStudio and initiated the session by executing the code shown above, you are good to go.īefore turning to the graphs, we load the data that we will display. # activate klippy for copy-to-clipboard button Now that we have installed the packages, we activate them as shown below. # install klippy for copy-to-clipboard button in code chunks To install the necessary packages, simply run the following code - it may take some time (between 1 and 5 minutes to install all of the libraries so you do not need to worry if it takes some time). If you have already installed the packages mentioned below, then you can skip ahead and ignore this section. Before turning to the code below, please install the packages by running the code below this paragraph. For this tutorials, we need to install certain packages from an R library so that the scripts shown below are executed without errors. If you have not installed R or are new to it, you will find an introduction to and more information how to use R here. This interactive Jupyter notebook allows you to execute code yourself and you can also change and edit the notebook, e.g. you can change code and upload your own data. If you want to render the R Notebook on your machine, i.e. knitting the document to html or a pdf, you need to make sure that you have R and RStudio installed and you also need to download the bibliography file and store it in the same folder where you store the Rmd file.Ĭlick this link to open an interactive version of this tutorial on. The entire R Notebook for the tutorial can be downloaded here.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |