Reputation:
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"))
Upvotes: 0
Views: 322
Reputation: 10291
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:
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:
Alternatively, you may find it easier to create an interaction variable as suggested by some of the other answers here.
Upvotes: 0
Reputation: 177
Your dput
had an incorrect format. I manually created it using the following:
df <- data.frame(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))
Then, I added a new column to your data which determines the line type based on the interaction of ParentSite.x
and FragSite.x
using the tidyverse
package:
df <- df %>%
mutate(linetype = ifelse(ParentSite.x=='Inner Bay'&FragSite.x=='Inner Bay' | ParentSite.x=='Outer Bay'&FragSite.x=='Outer Bay'
, 'Matching', 'Not Matching'))
Then, we plot:
ggplot(df, aes(x = TotalSA, y = T03SA, color = FragSite.x, linetype = linetype)) +
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")) +
labs(linetype='Line Type')
Upvotes: 0
Reputation: 602
Another way would be to create an additional variable that codes the linetype
. The effect is that you get a legend that show the two factors indedendetly:
M3 <- 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))
M3$in.out <- interaction(M3$ParentSite.x, M3$FragSite.x)
M3$comb <- 'same'
M3$comb[which(M3$ParentSite.x!=M3$FragSite.x)] <- 'different'
GPBWM03 = ggplot(M3, aes(x = TotalSA, y = T03SA,
group = interaction(FragSite.x, ParentSite.x), color = FragSite.x,
linetype = comb)) +
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"))
GPBWM03
Upvotes: 1