0

I'm very new to R and I hope this question is still interesting enough. I have the following dataframe:

> dput(df)
structure(list(Proportion = c(0.491475825983558, 0.624947117938639, 
0.284285973983444, 0.459936074937072, 0.438167575182789, 0.5923527, 
0.269347638359089, 0.444195335296524, 0.472343382529259, 0.6119936, 
0.280545311041942, 0.45582336843016), Lower = c(0.373501802431026, 
0.506815311121949, 0.196793171052086, 0.344394223066228, 0.342020291619279, 
0.4962054, 0.197239652248339, 0.347543569904938, 0.362690139261045, 
0.5158463, 0.198654362934906, 0.347479674558168), Upper = c(0.610508712286318, 
0.729864865043791, 0.39179224043653, 0.580031198686217, 0.539194328764963, 
0.6885, 0.356122647401151, 0.545263076314964, 0.5847316572176, 
0.7081409, 0.380178492952045, 0.56851602179505), Area = c("SNP", 
"SNP", "LGCA", "LGCA", "SNP", "SNP", "LGCA", "LGCA", "SNP", "SNP", 
"LGCA", "LGCA"), Time = c("Day", "Night", "Day", "Night", "Day", 
"Night", "Day", "Night", "Day", "Night", "Day", "Night"), Collar = c(41361, 
41361, 41361, 41361, 41365, 41365, 41365, 41365, 41366, 41366, 
41366, 41366)), row.names = c(NA, -12L), class = c("tbl_df", 
"tbl", "data.frame"))

For which I have created the following plot: plot

Using the script below:


dfnew <- df %>% 
  mutate(ymin = Proportion - Lower,
         ymax = Proportion + Upper)

p <- ggplot(data = dfnew, aes(x = Time, y = Proportion, color=Area, group=Area)) +
  geom_point(size = 6, stroke = 0, shape = 16, 
             position = position_dodge(width = 0.1))+
  geom_errorbar(aes(ymin = Lower, ymax = Upper), width=0.1, size=1,
                position = position_dodge(width = 0.1)) + 
  theme(axis.text=element_text(size=15),
        axis.title=element_text(size=20)) +
  scale_color_manual(values = c("SNP" = "coral", 
                                "LGCA" = "darkgoldenrod2")) +
  geom_line(size=1,linetype="dotted")
p

I would like plot different symbols (e.g. ∆, O, ◊) accounting for the different collars in df. Also, I would like these to be moved slightly (position_dodge) so that not all points are on top of each other.

How can I access a symbol library and implement it into my script?

Any help would be very appreciated!

juansalix
  • 503
  • 1
  • 8
  • 21

1 Answers1

3

If you map the shape within an aes() call you can vary the shapes and if you want specific shapes you can use scale_shape_manual() for example just like with the colors. The dodging within one group can be achieved by either using geom_jitter() or replacing position_dodge() with position_jitterdodge(). Unfortunately this messes with the errorbars.

EDIT: There is a fix for the error bars in this answer by Marcelo. I also included a way to connect the same symbols with the dotted line. This is easiest done by adding another grouping column to your data.

dfnew <- df %>% 
  mutate(ymin = Proportion - Lower,
         ymax = Proportion + Upper,
         linegroup = paste(Area, Collar))

set.seed(2)
myjit <- ggproto("fixJitter", PositionDodge,
                 width = 0.6,
                 dodge.width = 0,
                 jit = NULL,
                 compute_panel =  function (self, data, params, scales) 
                 {

                   #Generate Jitter if not yet
                   if(is.null(self$jit) ) {
                     self$jit <-jitter(rep(0, nrow(data)), amount=self$dodge.width)
                   }

                   data <- ggproto_parent(PositionDodge, self)$compute_panel(data, params, scales)

                   data$x <- data$x + self$jit
                   #For proper error extensions
                   if("xmin" %in% colnames(data)) data$xmin <- data$xmin + self$jit
                   if("xmax" %in% colnames(data)) data$xmax <- data$xmax + self$jit
                   data
                 } )


ggplot(data = dfnew, aes(x = Time, y = Proportion, color=Area, group=linegroup)) +
  geom_point(aes(shape = as.character(Collar)), size = 6, stroke = 0, 
             position = myjit)+
  geom_line(aes(group = linegroup),linetype = "dotted",size=1, position = myjit) +
  theme(axis.text=element_text(size=15),
        axis.title=element_text(size=20)) +
  geom_errorbar(aes(ymin = Lower, ymax = Upper), width=0.3, size=1,
                position = myjit) +
  scale_color_manual(values = c("SNP" = "coral", 
                                "LGCA" = "darkgoldenrod2"))

enter image description here

Mojoesque
  • 1,166
  • 8
  • 15
  • Thank you for your reply. Do you know how the dotted line can be implemented between the same type of symbol between factors Day and Night?Is there a way to do that? – juansalix Sep 05 '19 at 10:54
  • I edited the answer to fix the errorbars and add the dotted lines per symbol. If you play around with the numbers in myjit you can adjust the appearance to your liking. – Mojoesque Sep 05 '19 at 11:11
  • I'm trying to edit legends by having `Collar ID` instead of `as.character(Collar)`. I'm using `+ labs(fill = "Collar ID")` but that isn't making any difference. Any idea? – juansalix Sep 05 '19 at 11:22
  • 1
    It should be `shape = "Collar ID"` not fill. – Mojoesque Sep 05 '19 at 11:25
  • Great. That worked. Thank you for your help. Just one last question, how can I change the legend under `Collar ID` to get 41361´ instead of 41361, 41365´ instead of 41365 etc. for all three `Collar ID`s? In other words, adding a ´ to the text? Should that be done in the original dataframe? The problem is using ´ sign is gives problems in R.. – juansalix Sep 05 '19 at 11:37
  • 1
    Manipulating the data would be the easiest way. If that is not feasible you can use a `scale_shape_manual()` call with the labels argument. – Mojoesque Sep 05 '19 at 12:01