如何根据单独的组躲避geom_line

时间:2019-05-19 02:09:15

标签: r ggplot2

我正在尝试绘制此图,每条细线代表P03pd$ParentID,粗线代表基于P03pd$ParentSite的平均值。我想做的是基于ParentID避开每条ParentSite行。基本上,我希望所有蓝线都在同一方向上闪避,而所有橙线都在另一方向上闪避。误差线和线条似乎并没有闪避。下面的代码和我为该图提供的内容。

P03pd <- structure(list(ParentID = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
                                      10L, 1L, 2L, 7L, 9L, 4L, 6L, 10L, 8L, 5L, 3L, 12L, 11L, 17L, 13L, 14L, 
                                      16L, 19L, 15L, 18L, 20L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 
                                      20L), 
                                    .Label = c("65", "96", "154", "152", "98", "153", "151", "156", 
                                               "158", "157", "57", "56", "58", "59", "49", "48", "50", "60", "47", "54"), 
                                    class = "factor"), 
               FragSite = c("Inner Bay", "Inner Bay", "Inner Bay", "Inner Bay", "Inner Bay", 
                            "Inner Bay", "Inner Bay", "Inner Bay", "Inner Bay", "Inner Bay", 
                            "Outer Bay", "Outer Bay", "Outer Bay", "Outer Bay", 
                            "Outer Bay", "Outer Bay", "Outer Bay", "Outer Bay", "Outer Bay", 
                            "Outer Bay", "Inner Bay", "Inner Bay", "Inner Bay", "Inner Bay", 
                            "Inner Bay", "Inner Bay", "Inner Bay", "Inner Bay", "Inner Bay", 
                            "Inner Bay", "Outer Bay", "Outer Bay", "Outer Bay", "Outer Bay", 
                            "Outer Bay", "Outer Bay", "Outer Bay", "Outer Bay", "Outer Bay", 
                            "Outer Bay"), 
               ParentSite = c("Inner Bay", "Inner Bay", "Inner Bay", 
                              "Inner Bay", "Inner Bay", "Inner Bay", "Inner Bay", "Inner Bay", 
                              "Inner Bay", "Inner Bay", "Inner Bay", "Inner Bay", "Inner Bay", 
                              "Inner Bay", "Inner Bay", "Inner Bay", "Inner Bay", "Inner Bay", 
                              "Inner Bay", "Inner Bay", "Outer Bay", "Outer Bay", "Outer Bay", 
                              "Outer Bay", "Outer Bay", "Outer Bay", "Outer Bay", "Outer Bay", 
                              "Outer Bay", "Outer Bay", "Outer Bay", "Outer Bay", "Outer Bay", 
                              "Outer Bay", "Outer Bay", "Outer Bay", "Outer Bay", "Outer Bay", 
                              "Outer Bay", "Outer Bay"),
               T03pd = c(0.23974049125, 0.2961170863, 
                         0.2983113186, 0.3878869508, 0.412725752, 0.424192313444444, 
                         0.425082972, 0.503030865, 0.513017492833333, 0.572306859, 
                         0.389921224, 0.41408146925, 0.458399826333333, 0.5367372195, 
                         0.6343202141, 0.636154446, 0.643122098444444, 0.6841461515, 
                         0.7315538175, 0.749165482777778, 0.2389931862, 0.3307134997, 
                         0.3371411743, 0.345074892111111, 0.366728264428571, 0.3793964112, 
                         0.4263387348, 0.447909242375, 0.508916869833333, 0.640847461111111, 
                         0.419423126625, 0.44036607, 0.489692925888889, 0.518771272222222, 
                         0.5608911195, 0.5762836159, 0.6011474215, 0.628683831625, 
                         0.693109699666667, 0.832990982888889), 
               SEMT03pd = c(0.045511961346447, 
                            0.0510310948743258, 0.0305088388279076, 0.0392452011957092, 
                            0.0508805217965431, 0.0479578785301929, 0.0620734440790534, 
                            0.0718898914247557, 0.0362643618706244, 0.032826552837043, 
                            0.061150268265992, 0.0498908877302151, 0.0639530096035726, 
                            0.108997079901953, 0.0712411479086815, 0.105959932670364, 
                            0.0607702239968804, 0.0548526795173569, 0.0843205175910848, 
                            0.0857001372651065, 0.0370728291551275, 0.0303940226025309, 
                            0.0473366573552377, 0.0338549859651683, 0.0571861190913739, 
                            0.0644762222538039, 0.0418182180152785, 0.0568105354116434, 
                            0.0835977869503613, 0.0496939637824968, 0.0756509836859618, 
                            0.0676850683784375, 0.0521379746904529, 0.0589675720599358, 
                            0.0677800846270815, 0.0769734361819572, 0.0334209728670895, 
                            0.117769467411744, 0.122844589474382, 0.0906295354545253)), 
          class = "data.frame", 
          row.names = c(8L, 9L, 4L, 2L, 10L, 3L, 1L, 5L, 7L, 6L, 18L, 
                        19L, 11L, 17L, 12L, 13L, 16L, 15L, 20L, 14L, 26L, 27L, 24L, 28L, 29L, 22L, 
                        21L, 23L, 30L, 25L, 37L, 36L, 38L, 39L, 33L, 32L, 34L, 40L, 31L, 35L))
library(ggplot2)
pd03RxnNorm <- ggplot(P03pd, 
                     aes(x = FragSite, 
                         y = T03pd, 
                         color = ParentSite)) + 
  geom_line(aes(group = ParentID), 
            size = 0.1, position = position_dodge(width = 0.1)) + 
  geom_errorbar(aes(ymin = T03pd-SEMT03pd, ymax = T03pd+SEMT03pd), 
                width = .04, size = 0.15, 
                position = position_dodge(width = 0.1)) +
  stat_summary(aes(group = ParentSite), 
               fun.y = mean, 
               geom = "point", size = 0.75, 
               position = position_dodge(width = 0.1)) +
  stat_summary(aes(group = ParentSite), 
               fun.y = mean, geom = "line", size = 0.75, 
               position = position_dodge(width = 0.1)) + 
  stat_summary(fun.data = mean_se, geom = "errorbar", 
               width = 0.1, size = 0.75,
               position = position_dodge(width = 0.1)) + 
  scale_y_continuous(limits = c(0, 1)) + 
  scale_color_manual(values = c("Inner Bay" = "coral2",
                                "Outer Bay" = "skyblue4")) 

pd03RxnNorm = pd03RxnNorm + 
  theme(aspect.ratio = 1, 
       axis.text=element_text(size = 10), 
       axis.title=element_text(size = 10), 
       plot.title = element_text(hjust = 0.5, size = 10, face = "italic"), 
       panel.grid.major = element_blank(), 
       panel.grid.minor = element_blank(), 
       panel.background = element_blank(), 
       panel.border = element_blank(), 
       axis.line = element_line(colour = "black"), 
       legend.title = element_text(size = 8, hjust = 0.5),
       legend.key = element_blank(), 
       legend.text = element_text(size = 8),
       legend.position = c(0.75, 0.2), 
       legend.key.size = unit(5, "mm"), 
       legend.background = element_rect(fill = "white", linetype = "solid",
                                        colour = "black", size = 0.25)) + 
  scale_fill_manual(values = alpha(c("coral2", "skyblue4")),
                    name = "Origin Site", labels = c("Inner Bay Origin", 
                                                     "Outer Bay Origin")) + 
  labs(y = expression(atop("Calcification",
                           paste(('%'~day^-1%+-%SEM)))), 
       x = "Transplant Site",
       color = "Origin Site", title = "Porites compressa")

pd03RxnNorm

enter image description here

0 个答案:

没有答案