This article describes how to go from a table of long-format aggregate data:
to a custom-built stacked area chart, like this:
Requirements
Your data should be set up in a long format, with two numeric columns which will become the x and y axes of the chart, and one categorical column which will determine the categories the data is split by. See the supplemental section of How to Create Long-Format Aggregated Data Using R.
Method 1 - A Basic Chart
1. From the toolbar, click Calculation > Custom Code and click on the Page to place the object.
2. Paste the following into the R Code editor:
# Load the ggplot2 function library
library(ggplot2)
# Take an existing R Output or data frame called df which should be formatted as above.
data <- df
# Set up the plot itself and define where the data should come from. In the example here, the data
# for my x-axis is in Date, y-axis shows number of drinks in column 3 (y=data[,3]), and
# the categories are filled in by Age
plot = ggplot(data, aes(x=Date, y=data[,3], fill=Age))
# Print the plot to screen
plot + geom_area()Method 2 - Adding Formatting
Take the same steps as above, but replace the last line of code with more advanced formatting options, e.g., like this:
plot <- plot +
geom_area(colour="black", size=0.2, alpha=.8) + #outline the areas in black
theme_bw() + #apply black and white theme
labs(fill = "Age") + #label the legend
scale_x_continuous(breaks=seq(1,12,1)) + #set the x axis ticks
ggtitle("Total Number of Cola Drinks had Jan to Dec") + #give plot a title
labs(x="Month", y="Sum of Cola Drinks had") #give axes titlesThe above uses a range of options that are available when working with ggplot2. For more information on what these, see the official ggplot2 documentation
Next
How to Create Chart Templates Using R Functions
UPCOMING WEBINAR: 10 Market Research Predictions Over the Next 4 Years