David Bradshaw
David Bradshaw

Reputation: 31

Plotting using geom_smooth or stat_smooth

I am trying to plot a linear regression with a best fit line and 95% prediction lines, but when using stat_smooth or geom_smooth I get the graph seen in the picture. The lines do not show up on the graph and it seems to be trying to make those lines for all the Sites. A mock up of the data is below and in picture. Thank you for your time and help.

Site Cu  Fe
A     1 123
B     2 123
C     3 534
D     4 364
E     5 234
F     6 634
G     7 784
H     8 856

Screenshot: enter image description here

Upvotes: 1

Views: 9049

Answers (1)

Dan
Dan

Reputation: 1778

You're trying to do a regression out of one observation with color=site, that's why you're not getting any lines returned.

Here's the line of best fit with the 95% confidence level interval for predictions:

library(ggplot2)

# produce data
df1 <- structure(list(Site = c("A", "B", "C", "D", "E", "F", "G", "H"), Cu = 1:8, Fe = c(123L, 123L, 534L, 364L, 234L, 634L, 784L, 856L)), .Names = c("Site", "Cu", "Fe"),  row.names = c(NA, -8L), class = "data.frame")

# ordinary case of linear regression with 95CI band
ggplot(data = df1, aes( x = Fe, y = Cu)) +
  geom_point() +
  geom_smooth(method="lm")

enter image description here

If you still want to force the points to have a color legend, you can do:

# plot regression line with band and color points by Site
ggplot(data = df1, aes( x = Fe, y = Cu)) +
  geom_smooth(method="lm") +
  geom_point(aes(color=Site))

enter image description here

Since you have [only one] observation per site, I'd suggest that you label the points instead of mapping the geom_point to a color:

ggplot(data = df1, aes(x = Fe, y = Cu)) +
   geom_smooth(method = "lm") +
   geom_label(aes(label=Site))

enter image description here

Another option could be that you want to plot a line per Site, and your mock-up dataset is incomplete, in that case:

df1 <- data.frame(    Site = sample(letters[1:8], 999, replace=T),
                    Fe = runif(999), 
                    Cu = 1:999+rnorm(1))


ggplot(data = df1, aes(x = Fe, y = Cu, colour=Site)) +
  geom_smooth(method = "lm", alpha=0.1) +
  geom_point(alpha=0)

enter image description here

Upvotes: 5

Related Questions