Adding a Nonlinear Line to a Stacked Bar Plot in R
======================================================
In this post, we will explore how to add a nonlinear line to a stacked bar plot using the LOESS (Locally Estimated Scatterplot Smoothing) regression technique. This is achieved by taking the mean y-value of each box and then creating a smooth curve through these points.
Introduction
R provides several options for visualizing data, including bar plots, scatter plots, and line plots. However, when working with stacked bar plots, adding a nonlinear curve to the plot can help identify patterns and trends in the data that are not immediately apparent from the plot itself. In this post, we will explore how to achieve this using the LOESS regression technique.
Understanding Stacked Bar Plots
A stacked bar plot is used to compare the relative sizes of different categories or groups within a dataset. Each category is represented by a series of bars that are stacked on top of one another. The height of each bar represents the value for that category, while the width of the bar represents the proportion of data in that category.
In this specific case, we have a supply cost curve with dark bars representing the range in uncertainty in their costs (costrange). We want to add a curved line to fit the dark bars and provide a visual representation of the overall trend in the data.
Calculating the Mean Y-Value
To create a smooth curve through the points, we first need to calculate the mean y-value for each box. This can be achieved using the mean() function in R.
# Calculate the mean y-value for each box
mean.cost <- (costmat[1,]+colSums(costmat))/2
Creating a Smooth Curve
To create a smooth curve through these points, we will use the LOESS regression technique. This involves performing a linear regression on each data point individually and then interpolating between them to produce a smooth curve.
# Perform a LOESS regression
model <- loess(mean.cost ~ bp, span = 1)
# Predict values in the 0:100 range
pr <- predict(model, newdata = data.frame(bp = 0:100))
Plotting the Curve
To plot the curve, we can use the lines() function in R.
# Plot the smooth curve through the points
lines(0:100, pr, col = "red", lwd = 2)
Combining the Code
Here is the complete code for creating a stacked bar plot with a nonlinear line using the LOESS regression technique:
costtrans <- c(10,10,20,28,30,37,50,50,55,66,67,70)
costrange <- c(15,30,50,21,50,20,30,40,45,29,30,20)
cost3 <- table(costtrans, costrange)
# Stack the bars above a certain level
costmat <- matrix(data = cost3, ncol = 12, byrow = FALSE)
Dark <- rgb(99/255, 99/255, 99/250, 1)
Transparent <- rgb(99/255, 99/255, 99/250, 0)
production <- c(31.6,40.9,3.7,3.7,1,0.3,1.105,0.5,2.3,0.7,0.926,0.9)
par(xaxs = 'i', yaxs = 'i')
par(mar = c(4, 6, 4, 4))
barplot(costmat, production, space = 0, main = "Supply Curve",
col = c(Transparent, Dark), border = NA, ylab = "Cost")
# Calculate the mean y-value for each box
mean.cost <- (costmat[1,]+colSums(costmat))/2
# Perform a LOESS regression
model <- loess(mean.cost ~ bp, span = 1)
# Predict values in the 0:100 range
pr <- predict(model, newdata = data.frame(bp = 0:100))
# Plot the smooth curve through the points
lines(0:100, pr, col = "red", lwd = 2)
This code creates a stacked bar plot with a nonlinear line using the LOESS regression technique. The resulting plot provides a clear visual representation of the overall trend in the data and can help identify patterns and trends that are not immediately apparent from the plot itself.
Conclusion
In this post, we explored how to add a nonlinear line to a stacked bar plot using the LOESS regression technique. We walked through each step of the process, including calculating the mean y-value for each box, performing a LOESS regression, and plotting the curve. The resulting plot provides a clear visual representation of the overall trend in the data and can help identify patterns and trends that are not immediately apparent from the plot itself.
References
- R Development Core Team (2019). R documentation. https://cran.r-project.org/manuals.html
- Venables, W. N., & Ripley, B. D. J. (2002). Modern statistical computing with R (2nd ed.). John Wiley and Sons Ltd.
Last modified on 2024-01-06