-
Notifications
You must be signed in to change notification settings - Fork 0
/
06-a_plot_to_choose_from.Rmd
executable file
·61 lines (40 loc) · 3.57 KB
/
06-a_plot_to_choose_from.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# A plot to choose from - Week 7A {#histogram}
You should aim to complete this chapter in Week 7, Semester 1
## Relationships, differences and distributions
As discussed in lectures, the plot you choose to produce should be influenced by the question you are asking.
If you are;
* Looking at differences between variables - make something like a box plot or bar chart
* Looking at relationships between variables - make a scatter plot
* Looking at the distribution of data within a variable - make a histogram
## My first histogram in R
Lets have a go at making a histogram. Log-in to your posit Cloud account and go to your `tribolium_parasites` project.
Add the following to your script and run it;
```{r}
# Making a histogram and saving it in an object
# Call the ggplot function and direct it to your data and define your x axis
size_histogram <- ggplot(data = tribolium_combo,aes(x = wing_case_size)) +
geom_histogram(bins = 6) # Tell ggplot that you want it to build a histogram with 6 equal sized bins
print(size_histogram) # Print your new figure
```
Your plot should appear in the plot tab in the bottom right panel of posit Cloud.
A side note on histograms and bins. You may have noticed that we have only specified our x axis variable. This is because R is essentially calculating a frequency for the y axis. How many beetles have a 2.1mm long wing case? How many beetles have a 2.4mm long wing case? This what the y axis is based on. We have asked R to split our x axis variable into 6 equal parts (bins). Have a go at changing the number of bins, what happens to the histogram?
Now have a go at making a histogram for the `parasite_burden` variable.
Have a look at both of your histograms, what do you think they are showing? We will come back to histograms and how to read them in lectures.
## Exporting your histogram {#export}
You are going to want to save your plots, you can use the `ggsave` function to do this. Try running the code below;
```{r}
# Saving outputs
ggsave("figures/tribolium_wincase_size_histogram.pdf", # Give R a path to save to and a file name
plot = size_histogram,# Tell R what to save - in this case your object
width = 15, # Set .pdf width
height = 10, # Set .pdf height
units = "cm", # Specify units for .pdf width and height
device = "pdf") # Tell R what file type to create, in this case a pdf
# Note, use trial and error to select a good width and height for your figure
```
Note that if you remove the `device = ` line `ggsave` will automatically create a `.png` file. You can also use `width = , height = , units = "cm"` arguments to specify the size of your figure in cm. You will need to adjust these by trial and error until the proportional sizing of your plot to axis and legend labels is pleasing. Save both of your histogram outputs in your `figures` folder.
## Before you leave!
You should have two histograms saved in your `figures` folder from todays session. Check that they are saved and look as you would expect. Make sure to log out of posit Cloud and make sure you save your script!
## References
Wickham, Hadley, Mara Averick, Jennifer Bryan, Winston Chang, Lucy D’Agostino McGowan, Romain François, Garrett Grolemund, et al. 2019. “Welcome to the tidyverse.” Journal of Open Source Software 4 (43): 1686. https://doi.org/10.21105/joss.01686. <br />
Wickham, Hadley, Winston Chang, Lionel Henry, Thomas Lin Pedersen, Kohske Takahashi, Claus Wilke, Kara Woo, Hiroaki Yutani, and Dewey Dunnington. 2021. Ggplot2: Create Elegant Data Visualisations Using the Grammar of Graphics. https://CRAN.R-project.org/package=ggplot2.