My problems is that I have two interaction plots, one for psychological and one for somatic complaints. I want to portray them next to each other using the code cowplot. I would like to have similar yaxis (e.g., 1.75-2.75). But when setting the yaxis similar, the interaction disappear.
The code looks like this with no specification of the y-axis since it will mess up the graph):
# Plot for psychological complaints plot_psychological <- ggplot(final_overall, aes(x = surveyyear, y = psycho, color = FAS_cat, group = FAS_cat)) + geom_smooth(method = "loess", span = .8, se = FALSE) + labs(x = "Survey Year", y = "Psychological Complaints", title = "Survey Year vs Psychological Complaints per FAS category") + scale_color_manual(values = c("Below Average" = "blue", "Average" = "green", "Above Average" = "red"), name = "Family SES categories") + scale_x_continuous(breaks = seq(2002, 2022, by = 4), limits = c(2002, 2022)) + theme_minimal()+ labs(color = "Family SES categories")
# Plot for somatic complaints plot_somatic <- ggplot(final_overall, aes(x = surveyyear, y = somatic, color = FAS_cat, group = FAS_cat)) + geom_smooth(method = "loess", span = .8, se = FALSE) + labs(x = "Survey Year", y = "Somatic Complaints", title = "Survey Year vs Somatic Complaints per FAS category") + scale_color_manual(values = c("Below Average" = "blue", "Average" = "green", "Above Average" = "red"), name = "Family SES categories") + scale_x_continuous(breaks = seq(2002, 2022, by = 4), limits = c(2002, 2022)) + theme_minimal()+ labs(color = "Family SES categories")
# Combine the plots with different y-axes library(cowplot)
# Combine plots with shared y-axis limits combined_plot <- plot_grid(plot_psychological, plot_somatic, nrow = 1, align = "v")
# Print the combined plot print(combined_plot)
I tried the following by adding these lines in the ggplot codes:
- scale_y_continuous(limits = c(1.75, 2.75)) –> failed
- coord_cartesian(ylim = c(1.75, 2.75)) –> failed
- ylim(1.75, 2.75) –> failed
With failing I mean that I have now only one line in the plot, instead of three (based on the variable ‘FAS_cat’).
Hilde Brons is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.