根据2个条件设置线型格式ggplot2

时间:2019-04-19 17:18:48

标签: r ggplot2 grouping line

我正在根据两个位置的互惠移植(来源->新的,来源->来源)绘制值。我想根据历史记录来格式化这些值的线型,其中origin-> origin是一条实线,而origin-> new是一条虚线。对于2个站点,“原始”和“新”的值不同。以下是数据的子集:

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))

我想要的是内湾->内湾和外湾->外湾为实线,内湾->外湾和外湾->内湾为虚线。到目前为止,我的情况如下:

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"))

3 个答案:

答案 0 :(得分:1)

另一种方法是创建一个附加变量,以对linetype进行编码。这样做的结果是,您得到了一个图例,可以直观地显示两个因素:

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

enter image description here

答案 1 :(得分:0)

这是你的追求吗?

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"))

要获得如下输出:

enter image description here

这是使用下面的数据集完成的,因为上面的原始dput输出中的某些值存在换行符:

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))

编辑:对于组合图例,除了线型外,手动分配颜色可能是最简单的:

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"))

输出:

enter image description here

或者,您可能会发现,如此处其他一些答案所建议的那样,创建交互变量会更容易。

答案 2 :(得分:0)

您的dput的格式不正确。我使用以下命令手动创建了它:

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))

然后,我在您的数据中添加了一个新列,该列根据使用ParentSite.x包的FragSite.xtidyverse的交互来确定线型:

df <- df %>%
  mutate(linetype = ifelse(ParentSite.x=='Inner Bay'&FragSite.x=='Inner Bay' | ParentSite.x=='Outer Bay'&FragSite.x=='Outer Bay'
                             , 'Matching', 'Not Matching'))

然后,我们绘制:

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')

enter image description here