Skip to content

Commit 786b113

Browse files
dual line plot done
1 parent 85f4683 commit 786b113

File tree

1 file changed

+10
-15
lines changed
  • 3-Data-Visualization/R/12-visualization-relationships

1 file changed

+10
-15
lines changed

3-Data-Visualization/R/12-visualization-relationships/README.md

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -134,22 +134,17 @@ For this dataset, nothing particularly stands out with regards to the number of
134134

135135
## Dual-line Plots
136136

137-
Try a multiline plot by superimposing two lineplots on top of each other, using Seaborn's 'despine' to remove their top and right spines, and using `ax.twinx` [derived from Matplotlib](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.twinx.html). Twinx allows a chart to share the x axis and display two y axes. So, display the yield per colony and number of colonies, superimposed:
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:
138138

139-
```python
140-
fig, ax = plt.subplots(figsize=(12,6))
141-
lineplot = sns.lineplot(x=honey['year'], y=honey['numcol'], data=honey,
142-
label = 'Number of bee colonies', legend=False)
143-
sns.despine()
144-
plt.ylabel('# colonies')
145-
plt.title('Honey Production Year over Year');
146-
147-
ax2 = ax.twinx()
148-
lineplot2 = sns.lineplot(x=honey['year'], y=honey['yieldpercol'], ax=ax2, color="r",
149-
label ='Yield per colony', legend=False)
150-
sns.despine(right=False)
151-
plt.ylabel('colony yield')
152-
ax.figure.legend();
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)
153148
```
154149
![superimposed plots](images/dual-line.png)
155150

0 commit comments

Comments
 (0)