Two time series grouped or stacked
In the first chapter, you learned how to use axis()
to plot two lines on the same graphic with different Y scales. Should you want to compare them, however, you may find other kind of graphs to be more insightful. One solution is to plot both time series as barcharts. There are two types:
- Grouped barchart: for a single period, there are as many bars as time series
- Stacked bar chart: for each period, there is a single bar, and each time series is represented by a portion of the bar proportional to the value of the time series at this date (i.e. the total at each period adds up to 100%)
You are provided with a dataset (portfolio
) containing the weights of stocks A (stocka
) and B (stockb
) in your portfolio for each month in 2016. You will use the barplot()
function to create both types of charts.
This exercise is part of the course
Visualizing Time Series Data in R
Exercise instructions
- Plot a stacked barplot for
stocka
andstockb
. - Plot a grouped barplot for
stocka
andstockb
by settingbeside
equal toTRUE
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Plot stacked barplot
# Plot grouped barplot