Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将自定义x轴添加到绘图ggplot2和y轴_R_Plot_Ggplot2 - Fatal编程技术网

R 将自定义x轴添加到绘图ggplot2和y轴

R 将自定义x轴添加到绘图ggplot2和y轴,r,plot,ggplot2,R,Plot,Ggplot2,我有以下代码: 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)) +

我有以下代码:

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轴,以获得预期的下图。x轴将分为4个分段的4个部分,分别占分段1、分段2、分段3和分段4轴长度的39%、23%、23%和15%。有人能想出办法来解决这个问题吗。我感谢所有回复,并期待您的答复

非常感谢马克·赫克曼为我的问题提供了有用的答案。我想再问一件事。我还想通过“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))
多谢各位


这是我在不进入自定义注释栏的情况下所能获得的最好结果

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"))
库(ggplot2)
图书馆(网格)
df=数据帧(时间=换算系数(0.5:9.5,每个=10)),
roi=rep(1:10,10),面积=runif(100,5.0,7.5))
df$时间=系数(df$时间,级别=版本(级别(df$时间)))

p1您需要使用
注释_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") 
#####你的情节
图书馆(GG2)

是的,这就是我想要的。非常感谢。@Mark,当我运行那个代码时,我得到了这个错误:层中的错误(数据=NULL,stat=StatIdentity,position=PositionIdentity,:未找到对象'axis.y.pos',我的绘图在x轴中没有任何内容。你知道为什么吗?非常感谢。@HoángThịMỹDungLê忘记删除常数。现在已修复。H。非常感谢:)对不起,我忘记做过。谢谢你的要求。在y轴上,我还有另一个问题要解决。你能不能花点时间再看一次我的问题(下面的内容),如果你能再帮我一次,那就太好了。如果没有,我仍然感谢你迄今为止所做的一切,并花时间回答我的问题。再次感谢!
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)