2016-08-07 146 views
1

我有以下代码:添加自定义的x轴绘制GGPLOT2和y轴以及

df=data.frame(time=as.factor(rep(0.5:9.5,each=10)), 
       roi=rep(1:10,10), 
       area=runif(100, 5.0, 7.5)) 

df$time=factor(df$time, levels=rev(levels(df$time))) 

ggplot(data=df, aes(x=factor(roi), y=time, fill = area)) + 
    theme_minimal() + coord_fixed(ratio=1) + 
    geom_tile(colour = NA, width = 1.5, height = 1) + 
    scale_fill_gradient(low="black",high="white") 

现在,我想删除x轴和添加一个新的已经预期到无花果。 x轴将被分成4个部分,分别对于Seg 1,Seg 2,Seg 3,Seg 4分别具有39%,23%,23%,15%轴长度的4个部分。有没有人有任何想法来解决它。我感谢所有回应,并期待着您的回答。

非常感谢马克赫克曼对我的问题有帮助的答案。我想再问一个问题。我也想通过“scale_y_discrete”修改y轴,代码运行良好,但y轴标签没有达到我的预期。我跑的代码是:

ggplot(data=df, aes(x=factor(roi), y=time, fill = area)) + theme_minimal() +coord_fixed(ratio=1) +geom_tile(colour = NA, width = 1.5, height = 1)+scale_fill_gradient(low="black",high="white") +scale_y_discrete(name="Time (min)",expand =c(0.01,0.1),breaks=c(1,2.5,5.0,7.5,10.0),labels=c(0,15,30,45,60)) 

非常感谢!

enter image description here

回答

1

您需要使用annotation_custom才能绘图到绘图区域之外。

#### your plot 

library(ggplot2) 

g <- ggplot(data=df, aes(x=factor(roi), y=time, fill = area)) + 
    theme_minimal() + coord_fixed(ratio=1) + 
    geom_tile(colour = NA, width = 1.5, height = 1) + 
    scale_fill_gradient(low="black",high="white") 

额外的代码:

library(grid) 

# remove axis 
g <- g + theme(axis.title.x=element_blank(), 
      axis.text.x=element_blank(), 
       axis.ticks.x=element_blank()) + 
      scale_x_discrete(expand = c(0,0)) 

# calculate segment coordinates 
segs <- c(.39, .23, .23, .15) 
segs_center <- cumsum(segs) - segs/2 
seg_ticks <- cumsum(segs)[1:3] 
seg_labels <- paste("Seg", seq_along(segs)) 

# create graphicaal objects and gather as tree 
grobs <- grobTree(linesGrob(c(0,1), c(.5,.5)), 
        segmentsGrob(x0=seg_ticks, x1=seg_ticks, y0=0, y1=1), 
        textGrob(x=segs_center, y=0, 
          label = seg_labels, hjust = .5, gp = gpar(cex =.7))) 

# insert grobsTree in as annotation 
g <- g + annotation_custom(grob = grobs, 
          ymin = -.2, ymax = .2, 
          xmin = .25, xmax = 10.75) 

# override clipping for plotting outside of plotting area 
gt <- ggplot_gtable(ggplot_build(g)) 
gt$layout$clip[gt$layout$name == "panel"] <- "off" 
grid.newpage() 
grid.draw(gt) 

enter image description here

+0

是啊,这就是我想要的。非常感谢。 –

+0

@马克,当我运行该代码,我得到这个错误: 错误层(数据=空,统计= StatIdentity,位置= PositionIdentity,: 对象'axis.y.pos'没有找到 和我有阴谋在x轴上没有任何东西你知道为什么吗?非常感谢 –

+0

@HoàngThịMỹDungLê忘记删除一个常量现在修复 –

1

这是一样好,我可以不用进入定制标注grobs得到。

library(ggplot2) 
library(grid) 
df=data.frame(time=as.factor(rep(0.5:9.5,each=10)), 
       roi=rep(1:10,10),area=runif(100, 5.0, 7.5)) 
df$time=factor(df$time, levels=rev(levels(df$time))) 
p1 <- ggplot(data=df, aes(x=factor(roi), y=time, fill = area)) + 
    theme_minimal() +coord_fixed(ratio=1) + 
    geom_tile(colour = NA, width = 1.5, height = 1)+ 
    scale_fill_gradient(low="black",high="white") + 
    scale_x_discrete(breaks = c(4,6,8,10), 
        labels = paste0("Seg",1:4)) + 
    theme(axis.title.x = element_blank(), 
     axis.ticks.x = element_line(size =1), 
     axis.text.x = element_text(hjust=c(2,1.5,1.5,1.5)), 
     plot.margin = unit(c(2,0,2,0), "lines")) 

here,如果你想看看绘制轴标签和刻度线customwise。