[R] annotation help in ggplot2

Moshiur Rahman mr@hm@nku|mrt @end|ng |rom gm@||@com
Fri Sep 20 10:40:02 CEST 2019


Dear ggplot2 experts,

I'm struggling to make a plot having family id in x-axis and female id
below that family id where each 4 families have a single female id.

I also need to add male id on top of each bar which I can do before
grid.arrange, but fail after doing it. So, any suggestions?

Please find below my codes and help me to complete it perfectly.
#data
data <- read.table("R-help_ggplot2.csv", header=TRUE, sep=",")
names(data)
#packages
library(ggplot2)
library(grid)
library(gridExtra)
library(scales)
library(ggpubr)
#plot codes
p1 =
ggplot(data,aes(x=factor(family),y=offs.surv.perct,fill=factor(treat)))+
    geom_bar(stat="identity", position="dodge")+
    #facet_wrap(~ Female)+
    geom_bar(stat="identity", position="dodge", colour="black",
show.legend= TRUE) + # show_guide = TRUE gives legend boarder
    #geom_hline(yintercept=15, size=0.5, linetype = 2)+ ### middle line
    scale_fill_manual(values=c("grey", "white")) + # grey80 is closer to
white than black
    xlab("Family") +
    ylab (expression(paste("Offspring survival rate (%)"))) +
    #coord_cartesian(xlim=c(0,40), ylim=c(0,60), clip="off")+
    scale_y_continuous(expand = c(0, 0), limits = c(0, 60))+
    theme(legend.title = element_text(colour="black", size=12))+
    theme(legend.text = element_text(colour="black", size=11))+
    theme(legend.background = element_blank())+
    theme(legend.key = element_blank())+
    theme(legend.box.background = element_blank())+
    theme(legend.key.size = unit(2, "mm"),legend.key.width =
unit(0.5,"cm"))+
    theme(legend.position=c(0.05,0.9),legend.direction
="vertical",legend.box = "vertical")+ # add legend on top
    theme(panel.background = element_rect(fill = "transparent"))+
    theme(axis.line = element_line(colour =
"black"),axis.text.x=element_text(size=10,
colour="black"),axis.text.y=element_text(size=10, colour="black"),
        axis.title.y = element_text(size=12, colour =
"black",margin=margin(0,5,0,0)),
        axis.title.x = element_blank(),
        strip.text.x = element_blank(),# remove top level title (high and
low)
        strip.background = element_blank(),# remove top level background
        panel.grid.major = element_blank(),# remove grid line within the
plot
        panel.grid.minor = element_blank(),
        panel.border = element_blank())
p1

### legend correction
p2<- p1 + guides(fill = guide_legend(title="Fertilization group",keywidth =
1,keyheight = 1,
                             title.theme = element_text(size=12, colour =
"black", angle = 0)))+
  theme(legend.text = element_text(size = 12, colour = "black", angle = 0))

p2

###plot margins
p3<-p2+theme(plot.margin = unit(c(1,1,1.7,1), "lines"))
p3

#x-axis label
p4<-grid.arrange(
  p3,
  bottom = textGrob(
    "Family and female number",
    gp = gpar(fontsize = 12, cex=1), #fontface = "bold",
    vjust = 0.5, hjust = 0.5,x = 0.52))

# annotation (NOT WORKING)
p5<-p4+annotate("text", label = "M1", x = 1, y = 25.03, fontface = 1,
size=3,angle = 90)
p5
p6<-p5+annotate("text", label = "M2", x = 2, y = 18.37, fontface = 1,
size=3,angle = 90)
p6
p7<-p6+annotate("text", label = "M3", x = 3, y = 21.7, fontface = 1,
size=3,angle = 90)
p7
p8<-p7+annotate("text", label = "M1-2-3", x = 4, y = 19.1, fontface= 1,
size=3,angle = 90)
p8
Fig1.1<-p8+annotate("segment", size=0.3, x=c(1,1,4),xend=c(1,4,4),
                         y= c(26,28,28), yend=c(28,28,21))+
  annotate("text",x=2.45,y=29.5,fontface = 1, size=3,angle =
90,label=c("F1"))

Fig1.1

Please find attached also my data.

Any assistance will be highly appreciated.

Regards,

Moshi


JSPS Postdoctoral Fellow
Laboratory of Population Biology
Department of Marine Biosciences
Graduate School of Marine Science and Technology
Tokyo University of Marine Science and Technology
4-5-7 Konan, Minato-ku, Tokyo 108-8477, Japan
Mobile: 050-6874-9072


More information about the R-help mailing list