Search code examples
rggplot2facet-wrap

Tracing linear lines through same values in both axis when using facet_wrap in ggplot2


I have this dataframe:

df <- structure(list(Real = c(5, 11, 11, 17, 6, 7, 11, 11, 10, 11, 
13, 11, 7, 8, 8, 9, 10, 7, 7, 6, 5, 13, 8, 10, 7, 11, 4, 6, 5, 
7, 9, 4, 8), Predicted = c(7.53104960942489, 8.48933218073805, 
6.53527751338467, 15.5681067019339, 6.16286447784273, 9.60252366514763, 
9.42571629457248, 10.2524454322055, 9.32847653546665, 9.77523370651072, 
10.3625498760624, 14.275897279353, 7.69457109139684, 7.26906167591947, 
8.07512249762137, 7.36859387534307, 10.357436237549, 7.87435765638371, 
8.74311433109198, 7.29886361235044, 7.2713976024488, 7.55669872113011, 
7.43176240945188, 13.9422570019077, 9.88336055293638, 9.63284506089816, 
5.5285713215912, 10.4119762640024, 4.76896313240313, 8.45958527511939, 
5.65034115485842, 4.12733609483675, 4.27125334429662), Tooth = c("Incisor", 
"Incisor", "Incisor", "Incisor", "Incisor", "Canine", "Canine", 
"Canine", "Canine", "Canine", "Canine", "Canine", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Molar", "Molar", "Molar", "Molar", "Molar", "Molar", 
"Molar")), .Names = c("Real", "Predicted", "Tooth"), row.names = c("167", 
"174", "176", "315", "321", "105", "148", "241", "272", "305", 
"314", "337", "115", "118", "131", "141", "144", "224", "227", 
"249", "253", "258", "275", "306", "323", "338", "228", "295", 
"299", "311", "312", "317", "326"), class = "data.frame")

And I run this ggplot:

int_breaks <- function(x, n = 5) {
  l <- pretty(x, n)
  l[abs(l %% 1) < .Machine$double.eps ^ 0.5] 
}

library(ggplot2)
ggplot(data = df, aes(x = Real, y = Predicted)) +
  geom_point(color = "red") +
  facet_wrap(~factor(Tooth, levels = c("Incisor", "Canine", "Premolar", "Molar")), scales = "free") +
  scale_y_continuous(breaks = int_breaks) +
  scale_x_continuous(breaks = int_breaks)

I finally get this plot:

enter image description here

I would like to trace, in every plot, a line that passes by the same values of axis X and Y. That is, coordinates (10,10) and (15,15) would define the line.

The expected result would be something like this:

enter image description here

I tried with geom_abline() with no exit.

Moreover, I realized that when using coord_fixed(), I get an error. Any idea on how to standardized the visualization?

EDIT 1

Following Ian Campbell solution, copy-pasting his code, I get this:

enter image description here

I do not get any warning with that code. Any idea?


Solution

  • Does this approach with geom_abline not work for you?

    library(ggplot2)
    ggplot(data = df, aes(x = Real, y = Predicted)) +
      geom_point(color = "red") +
      geom_abline(slope = 1, intercept = 0) +
      facet_wrap(~factor(Tooth, levels = c("Incisor", "Canine", "Premolar", "Molar")), scales = "free") +
      scale_y_continuous(breaks = int_breaks) +
      scale_x_continuous(breaks = int_breaks)
    

    enter image description here