简体   繁体   中英

Fixing margins in ggplot2

I have a plot whose margins are slightly askew and need to be removed. So there should be no empty grey along the bottom or at the left and right of the plot. The dots should go right against the axis's. I think part of the problems may be my last two annotation lines. I tried adding + theme(plot.margins = unit(c(0.1,0.1,0.1,0.1), "cm")) in the last line that defines my plot but this just returns an error:

Error: Theme element `plot.margins` is not defined in the element hierarchy.

` 在此处输入图像描述

 pvalplot<-function(var, maintitle) {
    
    pvalall<-as.data.frame(c(t(var)))
    pvalall$Sample_Size<-c((1:(5*162)),(1:(11*162)),(1:(3*162)),(1:(5*162)),(1:(13*162)),(1:(3*162)),(1:(5*162)))
    pvalall$Domain<-c(rep("Physical",5*162),rep("Perinatal",11*162),rep("Developmental",3*162),
                      rep("Lifestyle-Life Events",5*162),rep("Parental-Family",13*162),rep("School",3*162),
                      rep("Neighborhood",5*162))
    pvalall$Domain <- factor(pvalall$Domain,
                             levels = c("Physical", "Perinatal", "Developmental", 
                                        "Lifestyle-Life Events", "Parental-Family",
                                        "School","Neighborhood"))
    pvalall[,1]<-ifelse(pvalall[,1]<1e-20,1e-20,pvalall[,1])
    
    names(pvalall)[1]<-"P-Values"
    
    pvalexp.labels= rep("",45*162)
    for (i in c(1:45)){
      j=i*162-81
      pvalexp.labels[j]=rownames(var)[i]
    } #makes list of empyt labels that w
    
    p<-ggplot(pvalall,aes(x = 1:nrow(pvalall), y = -log10(pvalall[,1])))+
      geom_point(aes(color = Domain,size=5),
                 alpha = 0.7, size=1)
    p+ylab(expression(atop(" -log10(P-Values)")))+
      ylim(0,20)+
      xlab(element_blank())+
      theme(legend.title=element_blank())+
      scale_x_continuous( breaks=c(1:45)*162-81, labels = rownames(var))+
      theme_classic()+
      theme(axis.title.x = element_blank())+
      theme(axis.ticks.x = element_blank())+
      theme(axis.title.y = element_text(size = 25))+
      theme(text = element_text(size=25))+
      theme(legend.title=element_blank())+
      theme(legend.position=c(0.8,0.7))+
      geom_abline(slope=0,intercept=-log10(c(var)[astsa::FDR(c(var))]),linetype = "dashed")+
      geom_abline(slope=0,intercept=5.2,linetype = "dashed")+
      ggtitle(maintitle)+
      theme(plot.title = element_text(hjust = 0.5))+
      theme(
        legend.box.background = element_rect(),
        legend.box.margin = margin(6, 6, 6, 6))+
      theme_update(axis.text.x = element_text(angle = 35, size = 6.5, vjust = 1, hjust=1, color = "black")) +
      annotate("text",  label = expression(paste("FDR P-value","=0.05")),size=5, x=1000,y=-log10(c(var)[astsa::FDR(c(var))])-0.5, color="black",parse=TRUE)+
      annotate("text",  label = expression(paste("Bonferroni P-value","=0.05")),size=5, x=1200,y=5.7, color="black",parse=TRUE)
  }#end of plotting

You can insert a line

scale_y_continuous(expand = c(0, 0)) +

in your plot command chain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM