Skip to content

Commit f2666cf

Browse files
Pie and Donut chart- code done
1 parent 17e79eb commit f2666cf

File tree

1 file changed

+35
-34
lines changed
  • 3-Data-Visualization/R-11-visualization-proportions

1 file changed

+35
-34
lines changed

3-Data-Visualization/R-11-visualization-proportions/README.md

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -58,34 +58,32 @@ The output is:
5858
```
5959
Take this data and convert the 'class' column to a category:
6060

61-
```python
62-
cols = mushrooms.select_dtypes(["object"]).columns
63-
mushrooms[cols] = mushrooms[cols].astype('category')
61+
```r
62+
grouped=mushrooms %>%
63+
group_by(class) %>%
64+
summarise(count=n())
6465
```
6566

66-
```python
67-
edibleclass=mushrooms.groupby(['class']).count()
68-
edibleclass
69-
```
7067

7168
Now, if you print out the mushrooms data, you can see that it has been grouped into categories according to the poisonous/edible class:
69+
```r
70+
View(grouped)
71+
```
72+
73+
74+
| class | count |
75+
| --------- | --------- |
76+
| Edible | 4208 |
77+
| Poisonous| 3916 |
7278

7379

74-
| | cap-shape | cap-surface | cap-color | bruises | odor | gill-attachment | gill-spacing | gill-size | gill-color | stalk-shape | ... | 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 |
75-
| --------- | --------- | ----------- | --------- | ------- | ---- | --------------- | ------------ | --------- | ---------- | ----------- | --- | ------------------------ | ---------------------- | ---------------------- | --------- | ---------- | ----------- | --------- | ----------------- | ---------- | ------- |
76-
| class | | | | | | | | | | | | | | | | | | | | | |
77-
| Edible | 4208 | 4208 | 4208 | 4208 | 4208 | 4208 | 4208 | 4208 | 4208 | 4208 | ... | 4208 | 4208 | 4208 | 4208 | 4208 | 4208 | 4208 | 4208 | 4208 | 4208 |
78-
| Poisonous | 3916 | 3916 | 3916 | 3916 | 3916 | 3916 | 3916 | 3916 | 3916 | 3916 | ... | 3916 | 3916 | 3916 | 3916 | 3916 | 3916 | 3916 | 3916 | 3916 | 3916 |
7980

80-
If you follow the order presented in this table to create your class category labels, you can build a pie chart:
81+
If you follow the order presented in this table to create your class category labels, you can build a pie chart.
8182

8283
## Pie!
8384

84-
```python
85-
labels=['Edible','Poisonous']
86-
plt.pie(edibleclass['population'],labels=labels,autopct='%.1f %%')
87-
plt.title('Edible?')
88-
plt.show()
85+
```r
86+
pie(grouped$count,grouped$class, main="Edible?")
8987
```
9088
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!
9189

@@ -97,26 +95,29 @@ A somewhat more visually interesting pie chart is a donut chart, which is a pie
9795

9896
Take a look at the various habitats where mushrooms grow:
9997

100-
```python
101-
habitat=mushrooms.groupby(['habitat']).count()
102-
habitat
98+
```r
99+
habitat=mushrooms %>%
100+
group_by(habitat) %>%
101+
summarise(count=n())
102+
View(habitat)
103103
```
104-
Here, you are grouping your data by habitat. There are 7 listed, so use those as labels for your donut chart:
104+
The output is:
105+
| habitat| count |
106+
| --------- | --------- |
107+
| Grasses | 2148 |
108+
| Leaves| 832 |
109+
| Meadows | 292 |
110+
| Paths| 1144 |
111+
| Urban | 368 |
112+
| Waste| 192 |
113+
| Wood| 3148 |
105114

106-
```python
107-
labels=['Grasses','Leaves','Meadows','Paths','Urban','Waste','Wood']
108115

109-
plt.pie(habitat['class'], labels=labels,
110-
autopct='%1.1f%%', pctdistance=0.85)
111-
112-
center_circle = plt.Circle((0, 0), 0.40, fc='white')
113-
fig = plt.gcf()
116+
Here, you are grouping your data by habitat. There are 7 listed, so use those as labels for your donut chart:
114117

115-
fig.gca().add_artist(center_circle)
116-
117-
plt.title('Mushroom Habitats')
118-
119-
plt.show()
118+
```r
119+
library(webr)
120+
PieDonut(habitat, aes(habitat, count=count))
120121
```
121122

122123
![donut chart](images/donut-wb.png)

0 commit comments

Comments
 (0)