Skip to content

Commit fd4bb32

Browse files
authored
Merge pull request #354 from jasleen101010/main
Add R for Data Visualizations- Lessons 11,12,13
2 parents 9ab24b5 + 786b113 commit fd4bb32

File tree

22 files changed

+522
-1
lines changed

22 files changed

+522
-1
lines changed
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
# Visualizing Proportions
2+
3+
|![ Sketchnote by [(@sketchthedocs)](https://sketchthedocs.dev) ](../../../sketchnotes/11-Visualizing-Proportions.png)|
4+
|:---:|
5+
|Visualizing Proportions - _Sketchnote by [@nitya](https://twitter.com/nitya)_ |
6+
7+
In this lesson, you will use a different nature-focused dataset to visualize proportions, such as how many different types of fungi populate a given dataset about mushrooms. Let's explore these fascinating fungi using a dataset sourced from Audubon listing details about 23 species of gilled mushrooms in the Agaricus and Lepiota families. You will experiment with tasty visualizations such as:
8+
9+
- Pie charts 🥧
10+
- Donut charts 🍩
11+
- Waffle charts 🧇
12+
13+
> 💡 A very interesting project called [Charticulator](https://charticulator.com) by Microsoft Research offers a free drag and drop interface for data visualizations. In one of their tutorials they also use this mushroom dataset! So you can explore the data and learn the library at the same time: [Charticulator tutorial](https://charticulator.com/tutorials/tutorial4.html).
14+
15+
## [Pre-lecture quiz](https://red-water-0103e7a0f.azurestaticapps.net/quiz/20)
16+
17+
## Get to know your mushrooms 🍄
18+
19+
Mushrooms are very interesting. Let's import a dataset to study them:
20+
21+
```r
22+
mushrooms = read.csv('../../data/mushrooms.csv')
23+
head(mushrooms)
24+
```
25+
A table is printed out with some great data for analysis:
26+
27+
28+
| class | cap-shape | cap-surface | cap-color | bruises | odor | gill-attachment | gill-spacing | gill-size | gill-color | stalk-shape | stalk-root | stalk-surface-above-ring | stalk-surface-below-ring | stalk-color-above-ring | stalk-color-below-ring | veil-type | veil-color | ring-number | ring-type | spore-print-color | population | habitat |
29+
| --------- | --------- | ----------- | --------- | ------- | ------- | --------------- | ------------ | --------- | ---------- | ----------- | ---------- | ------------------------ | ------------------------ | ---------------------- | ---------------------- | --------- | ---------- | ----------- | --------- | ----------------- | ---------- | ------- |
30+
| Poisonous | Convex | Smooth | Brown | Bruises | Pungent | Free | Close | Narrow | Black | Enlarging | Equal | Smooth | Smooth | White | White | Partial | White | One | Pendant | Black | Scattered | Urban |
31+
| Edible | Convex | Smooth | Yellow | Bruises | Almond | Free | Close | Broad | Black | Enlarging | Club | Smooth | Smooth | White | White | Partial | White | One | Pendant | Brown | Numerous | Grasses |
32+
| Edible | Bell | Smooth | White | Bruises | Anise | Free | Close | Broad | Brown | Enlarging | Club | Smooth | Smooth | White | White | Partial | White | One | Pendant | Brown | Numerous | Meadows |
33+
| Poisonous | Convex | Scaly | White | Bruises | Pungent | Free | Close | Narrow | Brown | Enlarging | Equal | Smooth | Smooth | White | White | Partial | White | One | Pendant | Black | Scattered | Urban
34+
| Edible | Convex |Smooth | Green | No Bruises| None |Free | Crowded | Broad | Black | Tapering | Equal | Smooth | Smooth | White | White | Partial | White | One | Evanescent | Brown | Abundant | Grasses
35+
|Edible | Convex | Scaly | Yellow | Bruises | Almond | Free | Close | Broad | Brown | Enlarging | Club | Smooth | Smooth | White | White | Partial | White | One | Pendant | Black | Numerous | Grasses
36+
37+
Right away, you notice that all the data is textual. You will have to convert this data to be able to use it in a chart. Most of the data, in fact, is represented as an object:
38+
39+
```r
40+
names(mushrooms)
41+
```
42+
43+
The output is:
44+
45+
```output
46+
[1] "class" "cap.shape"
47+
[3] "cap.surface" "cap.color"
48+
[5] "bruises" "odor"
49+
[7] "gill.attachment" "gill.spacing"
50+
[9] "gill.size" "gill.color"
51+
[11] "stalk.shape" "stalk.root"
52+
[13] "stalk.surface.above.ring" "stalk.surface.below.ring"
53+
[15] "stalk.color.above.ring" "stalk.color.below.ring"
54+
[17] "veil.type" "veil.color"
55+
[19] "ring.number" "ring.type"
56+
[21] "spore.print.color" "population"
57+
[23] "habitat"
58+
```
59+
Take this data and convert the 'class' column to a category:
60+
61+
```r
62+
library(dplyr)
63+
grouped=mushrooms %>%
64+
group_by(class) %>%
65+
summarise(count=n())
66+
```
67+
68+
69+
Now, if you print out the mushrooms data, you can see that it has been grouped into categories according to the poisonous/edible class:
70+
```r
71+
View(grouped)
72+
```
73+
74+
75+
| class | count |
76+
| --------- | --------- |
77+
| Edible | 4208 |
78+
| Poisonous| 3916 |
79+
80+
81+
82+
If you follow the order presented in this table to create your class category labels, you can build a pie chart.
83+
84+
## Pie!
85+
86+
```r
87+
pie(grouped$count,grouped$class, main="Edible?")
88+
```
89+
Voila, a pie chart showing the proportions of this data according to these two classes of mushrooms. It's quite important to get the order of the labels correct, especially here, so be sure to verify the order with which the label array is built!
90+
91+
![pie chart](images/pie1-wb.png)
92+
93+
## Donuts!
94+
95+
A somewhat more visually interesting pie chart is a donut chart, which is a pie chart with a hole in the middle. Let's look at our data using this method.
96+
97+
Take a look at the various habitats where mushrooms grow:
98+
99+
```r
100+
library(dplyr)
101+
habitat=mushrooms %>%
102+
group_by(habitat) %>%
103+
summarise(count=n())
104+
View(habitat)
105+
```
106+
The output is:
107+
| habitat| count |
108+
| --------- | --------- |
109+
| Grasses | 2148 |
110+
| Leaves| 832 |
111+
| Meadows | 292 |
112+
| Paths| 1144 |
113+
| Urban | 368 |
114+
| Waste| 192 |
115+
| Wood| 3148 |
116+
117+
118+
Here, you are grouping your data by habitat. There are 7 listed, so use those as labels for your donut chart:
119+
120+
```r
121+
library(ggplot2)
122+
library(webr)
123+
PieDonut(habitat, aes(habitat, count=count))
124+
```
125+
126+
![donut chart](images/donut-wb.png)
127+
128+
This code uses the two libraries- ggplot2 and webr. Using the PieDonut function of the webr library, we can create a donut chart easily!
129+
130+
Donut charts in R can be made using only the ggplot2 library as well. You can learn more about it [here](https://www.r-graph-gallery.com/128-ring-or-donut-plot.html) and try it out yourself.
131+
132+
Now that you know how to group your data and then display it as a pie or donut, you can explore other types of charts. Try a waffle chart, which is just a different way of exploring quantity.
133+
## Waffles!
134+
135+
A 'waffle' type chart is a different way to visualize quantities as a 2D array of squares. Try visualizing the different quantities of mushroom cap colors in this dataset. To do this, you need to install a helper library called [waffle](https://cran.r-project.org/web/packages/waffle/waffle.pdf) and use it to generate your visualization:
136+
137+
```r
138+
install.packages("waffle", repos = "https://cinc.rud.is")
139+
```
140+
141+
Select a segment of your data to group:
142+
143+
```r
144+
library(dplyr)
145+
cap_color=mushrooms %>%
146+
group_by(cap.color) %>%
147+
summarise(count=n())
148+
View(cap_color)
149+
```
150+
151+
Create a waffle chart by creating labels and then grouping your data:
152+
153+
```r
154+
library(waffle)
155+
names(cap_color$count) = paste0(cap_color$cap.color)
156+
waffle((cap_color$count/10), rows = 7, title = "Waffle Chart")+scale_fill_manual(values=c("brown", "#F0DC82", "#D2691E", "green",
157+
"pink", "purple", "red", "grey",
158+
"yellow","white"))
159+
```
160+
161+
Using a waffle chart, you can plainly see the proportions of cap colors of this mushrooms dataset. Interestingly, there are many green-capped mushrooms!
162+
163+
![waffle chart](images/waffle.png)
164+
165+
In this lesson, you learned three ways to visualize proportions. First, you need to group your data into categories and then decide which is the best way to display the data - pie, donut, or waffle. All are delicious and gratify the user with an instant snapshot of a dataset.
166+
167+
## 🚀 Challenge
168+
169+
Try recreating these tasty charts in [Charticulator](https://charticulator.com).
170+
## [Post-lecture quiz](https://red-water-0103e7a0f.azurestaticapps.net/quiz/21)
171+
172+
## Review & Self Study
173+
174+
Sometimes it's not obvious when to use a pie, donut, or waffle chart. Here are some articles to read on this topic:
175+
176+
https://www.beautiful.ai/blog/battle-of-the-charts-pie-chart-vs-donut-chart
177+
178+
https://medium.com/@hypsypops/pie-chart-vs-donut-chart-showdown-in-the-ring-5d24fd86a9ce
179+
180+
https://www.mit.edu/~mbarker/formula1/f1help/11-ch-c6.htm
181+
182+
https://medium.datadriveninvestor.com/data-visualization-done-the-right-way-with-tableau-waffle-chart-fdf2a19be402
183+
184+
Do some research to find more information on this sticky decision.
185+
## Assignment
186+
187+
[Try it in Excel](assignment.md)
14.7 KB
Loading
4.7 KB
Loading
7.43 KB
Loading
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
# Visualizing Relationships: All About Honey 🍯
2+
3+
|![ Sketchnote by [(@sketchthedocs)](https://sketchthedocs.dev) ](../../../sketchnotes/12-Visualizing-Relationships.png)|
4+
|:---:|
5+
|Visualizing Relationships - _Sketchnote by [@nitya](https://twitter.com/nitya)_ |
6+
7+
Continuing with the nature focus of our research, let's discover interesting visualizations to show the relationships between various types of honey, according to a dataset derived from the [United States Department of Agriculture](https://www.nass.usda.gov/About_NASS/index.php).
8+
9+
This dataset of about 600 items displays honey production in many U.S. states. So, for example, you can look at the number of colonies, yield per colony, total production, stocks, price per pound, and value of the honey produced in a given state from 1998-2012, with one row per year for each state.
10+
11+
It will be interesting to visualize the relationship between a given state's production per year and, for example, the price of honey in that state. Alternately, you could visualize the relationship between states' honey yield per colony. This year span covers the devastating 'CCD' or 'Colony Collapse Disorder' first seen in 2006 (http://npic.orst.edu/envir/ccd.html), so it is a poignant dataset to study. 🐝
12+
13+
## [Pre-lecture quiz](https://red-water-0103e7a0f.azurestaticapps.net/quiz/22)
14+
15+
In this lesson, you can use Seaborn, which you have used before, as a good library to visualize relationships between variables. Particularly interesting is the use of ggplot2's `ggplot`and `geom_point` function that allows scatter plots and line plots to quickly visualize '[statistical relationships](https://ggplot2.tidyverse.org/)', which allow the data scientist to better understand how variables relate to each other.
16+
17+
## Scatterplots
18+
19+
Use a scatterplot to show how the price of honey has evolved, year over year, per state. ggplot2, using `ggplot` and `geom_point`, conveniently groups the state data and displays data points for both categorical and numeric data.
20+
21+
Let's start by importing the data and Seaborn:
22+
23+
```r
24+
honey=read.csv('../../data/honey.csv')
25+
head(honey)
26+
```
27+
You notice that the honey data has several interesting columns, including year and price per pound. Let's explore this data, grouped by U.S. state:
28+
29+
| state | numcol | yieldpercol | totalprod | stocks | priceperlb | prodvalue | year |
30+
| ----- | ------ | ----------- | --------- | -------- | ---------- | --------- | ---- |
31+
| AL | 16000 | 71 | 1136000 | 159000 | 0.72 | 818000 | 1998 |
32+
| AZ | 55000 | 60 | 3300000 | 1485000 | 0.64 | 2112000 | 1998 |
33+
| AR | 53000 | 65 | 3445000 | 1688000 | 0.59 | 2033000 | 1998 |
34+
| CA | 450000 | 83 | 37350000 | 12326000 | 0.62 | 23157000 | 1998 |
35+
| CO | 27000 | 72 | 1944000 | 1594000 | 0.7 | 1361000 | 1998 |
36+
| FL | 230000 | 98 |22540000 | 4508000 | 0.64 | 14426000 | 1998 |
37+
38+
39+
Create a basic scatterplot to show the relationship between the price per pound of honey and its U.S. state of origin. Make the `y` axis tall enough to display all the states:
40+
41+
```r
42+
library(ggplot2)
43+
ggplot(honey, aes(x = priceperlb, y = state)) +
44+
geom_point(colour = "blue")
45+
```
46+
![scatterplot 1](images/scatter1.png)
47+
48+
Now, show the same data with a honey color scheme to show how the price evolves over the years. You can do this by adding a 'scale_color_gradientn' parameter to show the change, year over year:
49+
50+
> ✅ Learn more about the [scale_color_gradientn](https://www.rdocumentation.org/packages/ggplot2/versions/0.9.1/topics/scale_colour_gradientn) - try a beautiful rainbow color scheme!
51+
52+
```r
53+
ggplot(honey, aes(x = priceperlb, y = state, color=year)) +
54+
geom_point()+scale_color_gradientn(colours = colorspace::heat_hcl(7))
55+
```
56+
![scatterplot 2](images/scatter2.png)
57+
58+
With this color scheme change, you can see that there's obviously a strong progression over the years in terms of honey price per pound. Indeed, if you look at a sample set in the data to verify (pick a given state, Arizona for example) you can see a pattern of price increases year over year, with few exceptions:
59+
60+
| state | numcol | yieldpercol | totalprod | stocks | priceperlb | prodvalue | year |
61+
| ----- | ------ | ----------- | --------- | ------- | ---------- | --------- | ---- |
62+
| AZ | 55000 | 60 | 3300000 | 1485000 | 0.64 | 2112000 | 1998 |
63+
| AZ | 52000 | 62 | 3224000 | 1548000 | 0.62 | 1999000 | 1999 |
64+
| AZ | 40000 | 59 | 2360000 | 1322000 | 0.73 | 1723000 | 2000 |
65+
| AZ | 43000 | 59 | 2537000 | 1142000 | 0.72 | 1827000 | 2001 |
66+
| AZ | 38000 | 63 | 2394000 | 1197000 | 1.08 | 2586000 | 2002 |
67+
| AZ | 35000 | 72 | 2520000 | 983000 | 1.34 | 3377000 | 2003 |
68+
| AZ | 32000 | 55 | 1760000 | 774000 | 1.11 | 1954000 | 2004 |
69+
| AZ | 36000 | 50 | 1800000 | 720000 | 1.04 | 1872000 | 2005 |
70+
| AZ | 30000 | 65 | 1950000 | 839000 | 0.91 | 1775000 | 2006 |
71+
| AZ | 30000 | 64 | 1920000 | 902000 | 1.26 | 2419000 | 2007 |
72+
| AZ | 25000 | 64 | 1600000 | 336000 | 1.26 | 2016000 | 2008 |
73+
| AZ | 20000 | 52 | 1040000 | 562000 | 1.45 | 1508000 | 2009 |
74+
| AZ | 24000 | 77 | 1848000 | 665000 | 1.52 | 2809000 | 2010 |
75+
| AZ | 23000 | 53 | 1219000 | 427000 | 1.55 | 1889000 | 2011 |
76+
| AZ | 22000 | 46 | 1012000 | 253000 | 1.79 | 1811000 | 2012 |
77+
78+
79+
Another way to visualize this progression is to use size, rather than color. For colorblind users, this might be a better option. Edit your visualization to show an increase of price by an increase in dot circumference:
80+
81+
```r
82+
ggplot(honey, aes(x = priceperlb, y = state)) +
83+
geom_point(aes(size = year),colour = "blue") +
84+
scale_size_continuous(range = c(0.25, 3))
85+
```
86+
You can see the size of the dots gradually increasing.
87+
88+
![scatterplot 3](images/scatter3.png)
89+
90+
Is this a simple case of supply and demand? Due to factors such as climate change and colony collapse, is there less honey available for purchase year over year, and thus the price increases?
91+
92+
To discover a correlation between some of the variables in this dataset, let's explore some line charts.
93+
94+
## Line charts
95+
96+
Question: Is there a clear rise in price of honey per pound year over year? You can most easily discover that by creating a single line chart:
97+
98+
```r
99+
qplot(honey$year,honey$priceperlb, geom='smooth', span =0.5, xlab = "year",ylab = "priceperlb")
100+
```
101+
Answer: Yes, with some exceptions around the year 2003:
102+
103+
![line chart 1](images/line1.png)
104+
105+
Question: Well, in 2003 can we also see a spike in the honey supply? What if you look at total production year over year?
106+
107+
```python
108+
qplot(honey$year,honey$totalprod, geom='smooth', span =0.5, xlab = "year",ylab = "totalprod")
109+
```
110+
111+
![line chart 2](images/line2.png)
112+
113+
Answer: Not really. If you look at total production, it actually seems to have increased in that particular year, even though generally speaking the amount of honey being produced is in decline during these years.
114+
115+
Question: In that case, what could have caused that spike in the price of honey around 2003?
116+
117+
To discover this, you can explore a facet grid.
118+
119+
## Facet grids
120+
121+
Facet grids take one facet of your dataset (in our case, you can choose 'year' to avoid having too many facets produced). Seaborn can then make a plot for each of those facets of your chosen x and y coordinates for more easy visual comparison. Does 2003 stand out in this type of comparison?
122+
123+
Create a facet grid by using `facet_wrap` as recommended by [ggplot2's documentation](https://ggplot2.tidyverse.org/reference/facet_wrap.html).
124+
125+
```r
126+
ggplot(honey, aes(x=yieldpercol, y = numcol,group = 1)) +
127+
geom_line() + facet_wrap(vars(year))
128+
```
129+
In this visualization, you can compare the yield per colony and number of colonies year over year, side by side with a wrap set at 3 for the columns:
130+
131+
![facet grid](images/facet.png)
132+
133+
For this dataset, nothing particularly stands out with regards to the number of colonies and their yield, year over year and state over state. Is there a different way to look at finding a correlation between these two variables?
134+
135+
## Dual-line Plots
136+
137+
Try a multiline plot by superimposing two lineplots on top of each other, using R's `par` and `plot` function. We will be plotting the year in the x axis and display two y axes. So, display the yield per colony and number of colonies, superimposed:
138+
139+
140+
```r
141+
par(mar = c(5, 4, 4, 4) + 0.3)
142+
plot(honey$year, honey$numcol, pch = 16, col = 2,type="l")
143+
par(new = TRUE)
144+
plot(honey$year, honey$yieldpercol, pch = 17, col = 3,
145+
axes = FALSE, xlab = "", ylab = "",type="l")
146+
axis(side = 4, at = pretty(range(y2)))
147+
mtext("colony yield", side = 4, line = 3)
148+
```
149+
![superimposed plots](images/dual-line.png)
150+
151+
While nothing jumps out to the eye around the year 2003, it does allow us to end this lesson on a little happier note: while there are overall a declining number of colonies, the number of colonies is stabilizing even if their yield per colony is decreasing.
152+
153+
Go, bees, go!
154+
155+
🐝❤️
156+
## 🚀 Challenge
157+
158+
In this lesson, you learned a bit more about other uses of scatterplots and line grids, including facet grids. Challenge yourself to create a facet grid using a different dataset, maybe one you used prior to these lessons. Note how long they take to create and how you need to be careful about how many grids you need to draw using these techniques.
159+
## [Post-lecture quiz](https://red-water-0103e7a0f.azurestaticapps.net/quiz/23)
160+
161+
## Review & Self Study
162+
163+
Line plots can be simple or quite complex. Do a bit of reading in the [ggplot2 documentation](https://ggplot2.tidyverse.org/reference/geom_path.html#:~:text=geom_line()%20connects%20them%20in,which%20cases%20are%20connected%20together) on the various ways you can build them. Try to enhance the line charts you built in this lesson with other methods listed in the docs.
164+
## Assignment
165+
166+
[Dive into the beehive](assignment.md)
19.2 KB
Loading
39.4 KB
Loading
17.7 KB
Loading
17.6 KB
Loading
31.3 KB
Loading

0 commit comments

Comments
 (0)