【问题标题】:Fixing margins in ggplot2修复 ggplot2 中的边距
【发布时间】:2021-06-06 21:56:15
【问题描述】:

我有一个图,其边距略微歪斜,需要删除。因此,图的底部或左右两侧不应该有空的灰色。这些点应该正好靠在轴上。我认为部分问题可能是我的最后两个注释行。我尝试在定义我的情节的最后一行添加+ theme(plot.margins = unit(c(0.1,0.1,0.1,0.1), "cm")),但这只会返回一个错误:

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

【问题讨论】:

  • 这是plot.margin 而不是边距s。 (;

标签: r ggplot2 data-visualization visualization


【解决方案1】:

你可以插入一行

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

在您的绘图命令链中。

【讨论】:

  • 感谢您解决了这个问题但又引入了另一个问题,我也为 scale_x_continuous(breaks=c(1:45)*162-81, labels = rownames(var))+ 做到了maming it scale_x_continuous(breaks=c(1:45)*162-81, labels = rownames(var), expand =c(0,0)) 但现在我的标题图没有居中你会看到我添加了正确的hadjust 线​​,所以它应该可以工作。我尝试移动命令的顺序,但这也没有解决。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-16
  • 1970-01-01
  • 2021-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多