This article describes how go from a table of long-format aggregate data:
to a custom-built stacked area chart, like this:
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 (including the supplemental section).
Method 1 - A Basic Chart
1. Create an empty custom R Calculation. Click Calculation > Custom Code and draw a box on the Page.
2. In the object inspector > Properties > R CODE enter code as follows:
# Load the ggplot2 function library
# 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
1. Take the same steps as above, but replace the last line of code with more advanced formatting options, eg, 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 titles
The above uses a range of options that are available when working with ggplot2. For more information on what these, see the official ggplot2 documentation
How to Create Chart Templates Using R Functions
Article is closed for comments.