Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Visualizing Time Series Data in R

Cursus bekijken

Oefeninstructies

  • Plot a stacked barplot for stocka and stockb.
  • Plot a grouped barplot for stocka and stockb by setting beside equal to TRUE.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# Plot stacked barplot


# Plot grouped barplot
Code bewerken en uitvoeren