Reputation: 533
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:
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!
Upvotes: 0
Views: 1533
Reputation: 1318
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"))
Upvotes: 3