Search code examples
rggplot2groupingline

Format linetype based on 2 conditions ggplot2


I'm plotting values based on a reciprocal transplant (origin -> new, origin -> origin) for 2 sites. I want to format the linetype for these values based on history where origin -> origin is a solid line, but origin -> new is a dashed line. With 2 sites, the values for "origin" and "new" are different. Below is a subset of the data:

structure(list(FragID = c("1004", "1006", "1015", "1038", "1087", 
"1089", "1107", "1116"), ParentID = c("166", "166", "166", "166", 
"144", "144", "144", "144"), ParentSite.x = c("Inner Bay", "Inner 
Bay", "Inner Bay", "Inner Bay", "Outer Bay", "Outer Bay", "Outer 
Bay", "Outer Bay"), FragSite.x = c("Inner Bay", "Outer Bay", "Inner 
Bay", "Outer Bay", "Inner Bay", "Inner Bay", "Outer Bay", "Outer 
Bay"), TotalSA = c(0.0171970755726674, 0.0338197513082082, 
0.0215722601402604, 0.030712272182997, 0.027529366126288, 
0.029650482611575, 0.0316984120058258, 0.0356299659679559), T03SA = 
c(0.000709604810935872, 0.00148788795124323, 0.00109901406229665, 
0.000966259734683879, 0.000701528253168926, 0.000828107993427705, 
0.00079488114602085, 0.000904344998291552)), .Names = c("FragID", 
"ParentID", "ParentSite.x", "FragSite.x", "TotalSA", "T03SA"), 
class = "data.frame", row.names = c(2L, 3L, 5L, 9L, 21L, 22L, 28L, 
29L))

What I want is for Inner Bay -> Inner Bay and Outer Bay -> Outer Bay to be solid lines and Inner Bay -> Outer Bay and Outer Bay -> Inner Bay to be dashed. What I have so far is below:

GPBWM03 = ggplot(M3, aes(x = TotalSA, y = T03SA, 
                 group = interaction(FragSite.x, ParentSite.x), color = FragSite.x, 
                 linetype = ParentSite.x)) + 
  geom_point(alpha = 0.5, stroke = 0) +
  geom_smooth(method = "lm", formula = y~x, se = FALSE, fullrange = 
TRUE) + 
  scale_color_manual(values = c("Inner Bay" = "coral2", 
"Outer Bay" = "skyblue4"))

Solution

  • Is this what you're after?

    ggplot(M3,
           aes(
               x = TotalSA,
               y = T03SA,
               color = FragSite.x,
               linetype = interaction(ParentSite.x, FragSite.x)
           )) +
        geom_smooth(
            method = "lm",
            formula = y ~ x,
            se = FALSE,
            fullrange =
                TRUE
        ) +
        scale_linetype_manual(values = c(1, 2, 2, 1)) +
        scale_color_manual(values =
                               c("Inner Bay" = "coral2", "Outer Bay" = "skyblue4"))
    

    To get an output like:

    enter image description here

    This was made using the dataset below, as there were line breaks in some of the values in your original dput output above:

    structure(list(FragID = c("1004", "1006", "1015", "1038", "1087", 
    "1089", "1107", "1116"), ParentID = c("166", "166", "166", "166", 
    "144", "144", "144", "144"), ParentSite.x = c("Inner Bay", "Inner Bay", 
    "Inner Bay", "Inner Bay", "Outer Bay", "Outer Bay", "Outer Bay", 
    "Outer Bay"), FragSite.x = c("Inner Bay", "Outer Bay", "Inner Bay", 
    "Outer Bay", "Inner Bay", "Inner Bay", "Outer Bay", "Outer Bay"
    ), TotalSA = c(0.0171970755726674, 0.0338197513082082, 0.0215722601402604, 
    0.030712272182997, 0.027529366126288, 0.029650482611575, 0.0316984120058258, 
    0.0356299659679559), T03SA = c(0.000709604810935872, 0.00148788795124323, 
    0.00109901406229665, 0.000966259734683879, 0.000701528253168926, 
    0.000828107993427705, 0.00079488114602085, 0.000904344998291552
    )), class = "data.frame", row.names = c(2L, 3L, 5L, 9L, 21L, 
    22L, 28L, 29L))
    

    Edit: For combined legend, it may be easiest to just manually assign the colors in addition to the linetypes:

    ggplot(M3,
           aes(
               x = TotalSA,
               y = T03SA,
               color = interaction(ParentSite.x, FragSite.x),
               linetype = interaction(ParentSite.x, FragSite.x)
           )) +
        geom_smooth(
            method = "lm",
            formula = y ~ x,
            se = FALSE,
            fullrange =
                TRUE
        ) +
        scale_linetype_manual(name = "Combination", values = c(1, 2, 2, 1)) +
        scale_color_manual(name = "Combination", values = c("coral2", "coral2", "skyblue4", "skyblue4"))
    

    Output:

    enter image description here

    Alternatively, you may find it easier to create an interaction variable as suggested by some of the other answers here.