This is my first foray into ggplot2 and I am experiencing difficulties. I'm trying to plot two series of random numbers against an incremented x-axis while showing linear regression for both. So far, I've succeeded in plotting the scatterplots, but the regression line keeps throwing errors. I know it's possible, but I'm missing something for executing the idea. I'm running RStudio Desktop version 1.3.1056, Water Lily with tidyverse loaded.
I know this works to display the scatterplot (I'm open to more elegant variants if suggested):
ggplot(a, aes(x = Datapoint, y = value, color = variable)) + # Setup
geom_point(aes(y = Series1, col = 'Series1')) + # Series 1 plot
geom_point(aes(y = Series2, col = 'Series2')) + # Series 2 plot
labs(title = 'example', xlab = 'Datapoint', ylab = 'Datapoint Value') # Title and axes labels
I also know this works to display a linear regression line if only using one y-series:
ggplot(a) +
aes(x = Datapoint, y = value, color = variable) +
geom_point()
When I try adding geom_smooth()
or geom_smooth(method = lm)
to the main block, I end up getting a "Error in FUN(X[[i]], ...) : object 'value' not found" message. For example, this:
ggplot(a, aes(x = Datapoint, y = value, color = variable)) + # Setup
geom_point(aes(y = Series1, col = 'Series1')) + # Series 1 plot
geom_point(aes(y = Series2, col = 'Series2')) + # Series 2 plot
labs(title = 'example', xlab = 'Datapoint', ylab = 'Datapoint Value') + # Title and axes labels
geom_smooth(method = lm)
results in this:
> ggplot(a, aes(x = Datapoint, y = value, color = variable)) + # Setup
+ geom_point(aes(y = Series1, col = 'Series1')) + # Series 1 plot
+ geom_point(aes(y = Series2, col = 'Series2')) + # Series 2 plot
+ labs(title = 'example', xlab = 'Datapoint', ylab = 'Datapoint Value') + # Title and axes labels
+ geom_smooth(method = lm)
Error in FUN(X[[i]], ...) : object 'value' not found
Some places I've looked for inspiration include the following:
- Multiple linear regression for a dataset in R with ggplot2
- https://www.sixhat.net/how-to-plot-multpile-data-series-with-ggplot.html
- http://www.sthda.com/english/wiki/ggplot2-scatter-plots-quick-start-guide-r-software-and-data-visualization
- http://www.cookbook-r.com/Graphs/Scatterplots_(ggplot2)/
I'm fairly certain this should be a simple issue, but one which I don't yet understand. What am I missing?
The data file I'm using is hosted here: https://github.com/davidmvermillion/Chart_Comparisons/blob/master/Seeded_Values_for_Comparison_Project.csv
This is my current R file: https://github.com/davidmvermillion/Chart_Comparisons/blob/master/ggplot2Demo.R
Thank you!