The confidence interval on my scatter plot doesn't cover the entire x axis, while the regression line fits the entire data set. The data that isn't covered by the c. interval doesn't have any missing values or zeros.
I tried using fullrange = TRUE , but it doesn't work.
Here is the code I used to create the plot:
plot_wet_arthropoda <- ggplot(joined_data_arthropoda, aes(x = CCL_n_t, y = total_wet_mass_arthropoda)) +
geom_point(color = "blue", size = 3) +
geom_smooth(method = "lm", se = TRUE, fullrange = TRUE, color = "red" ) +
annotate("text", x = min(joined_data_arthropoda$CCL_n_t, na.rm = TRUE),
y = max(joined_data_arthropoda$total_wet_mass_arthropoda, na.rm = TRUE)*1.9,
label = annot_arth_wet, hjust = 0, size = 4) +
labs(
y = "Total Wet Arthropoda Weight (g)"
) +
scale_y_continuous(
breaks = c(50, 100, 200, 300, 400, 500, 600, 700, 800, 900),
limits = c(0, 900)) +
scale_x_continuous(
breaks = c(25, 30, 35, 40, 45, 50, 55)) +
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5))
As described in https://r4ds.hadley.nz/communication.html#zooming, setting limits in scale_x_* and scale_y_* will subset the data to the plotted elements that are fully within that range. So the confidence interval part of your geom_smooth will not show portions that exceed those ranges. Below, we see the shaded area stop right when it would go below 8.
In contrast, coord_cartesian lets you specify the "viewport" of what range should be visible, without removing plotted elements that exceed that range.
Compare:
library(ggplot2)
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
geom_smooth(method = "lm", se = TRUE, fullrange = TRUE) +
scale_y_continuous(limits = c(8, 35),
breaks = c(8, 10, 20, 30), minor_breaks = NULL)
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
geom_smooth(method = "lm", se = TRUE, fullrange = TRUE) +
coord_cartesian(ylim = c(8, 35))
Further, adding clip = "off" to coord_cartesian will show all the plotted data without clipping to the plot area. I use this often for geom_text layers where I want the text to overflow beyond the plot area without getting clipped.