Add regression line and equation just in the first line of a facet_grid

Hello everyone,

I am trying to add a regression line in a facet grid graphs, but only in the first line.
When I use the function geom_smooth() all graphs appear with the regression line.
I tryied to insert a "if" funtion, but gets me an error message.

Here is my code:
p <- ggplot(datar, aes(x = Ano, y = area_taxa,group = usos)) +
geom_line(aes(color = usos), size = 1) +
geom_smooth(method="lm", size = 0.8, lty = "dashed", color = "red") +
theme(legend.position = "none") +
theme(axis.text.y = element_text(size = 18, color = "black"),
axis.text.x = element_text(size=15, color = "black"),
axis.title.x = element_text(size = 20),
axis.title.y = element_text(size = 20),
legend.text = element_text(size=20),
strip.text.x = element_text(size = 18),
strip.text.y = element_text(size = 18)) +
labs(x = "Year", y = "") + ggtitle("") +
facet_grid(tipo_f~usos_f, scales = "free_y")+
scale_x_continuous(breaks = seq(1985,2020,10))

I tryied the add:
if(tipo_f = "(A) Land use (Mha)"){
geom_smooth(method="lm", size = 0.8, lty = "dashed", color = "red")}

I also need the equations to be shown in the graphs.

Can someone help me please?!

Try modifying your data prior to plotting, like so:

# Load libraries ----------------------------------------------------------
library("tidyverse")


# Create example data -----------------------------------------------------
n <- 100
set.seed(232153)
my_data <- tibble(
  x1 = rnorm(n),
  x2 = rnorm(n),
  g1 = sample(LETTERS[1:3], size = n, replace = TRUE),
  g2 = sample(LETTERS[4:6], size = n, replace = TRUE),
)


# Augment data ------------------------------------------------------------
my_data <- my_data %>% 
  mutate(x1_A_D = case_when(g1 == "A" & g2 == "D" ~ x1,
                            TRUE ~ NA_real_),
         x2_A_D = case_when(!is.na(x1_A_D) ~ x2,
                            TRUE ~ NA_real_))


# Visualise ---------------------------------------------------------------
my_data %>% 
  ggplot(aes(x = x1, y = x2)) + 
  geom_line() +
  geom_smooth(aes(x = x1_A_D, y = x2_A_D),
              method = "lm", linetype = "dashed") +
  theme_minimal() +
  facet_grid(vars(g2), vars(g1))

Yielding:

1 Like

Uaaau...it worked ver well!! Thank you!!
Can you also help me in showing the equations in the graphs, please?

Google is your friend: https://www.statology.org/add-regression-equation-to-plot-in-r/

1 Like

Thanks!! It worked too!! But now I am trying to break Y axis, to gave a better resolution, I am trying to find in google but still nothing, can you help me again, please?
I'd like to break just the second line of graphs.

You had this in your initial example, look at your "scales" in your facets

1 Like

Actually the initial example was incomplete, this one is the final plot. The broken axis is something I can try latter. Thank you very much for your help!! :relaxed:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.