r中的"scale"或"ruler"类型的图

SHR*_*ram 12 plot r scale ggplot2

我试图将标记放在条形内以创建条形图(水平或垂直)以提供像刻度的视图. 在此输入图像描述

只是一个小例子:

myd <- data.frame (names = c("A", "B", "C", "D"), height = c(2.1, 3.5, 3.5,1.5))
require(ggplot2)
c <- ggplot(myd, aes(factor(names), height, fill = names))
c + geom_bar()
c + geom_bar() + coord_flip()
Run Code Online (Sandbox Code Playgroud)

有没有一种简单的方法或包可以实现这一目标?

Did*_*rts 16

使用ggplot2库的相当长的解决方案.

首先修改你的数据框 - 重复每个元素namesheight按照0.2的次数发生在条的高度.

myd <- data.frame (names = c(rep("A",floor(2.1/0.2)), rep("B",floor(3.5/0.2)), rep("C",floor(3.5/0.2)), rep("D",floor(1.5/0.2))),
                   height = c(rep(2.1,floor(2.1/0.2)), rep(3.5,floor(3.5/0.2)), rep(3.5,floor(3.5/0.2)), rep(1.5,floor(1.5/0.2))))
Run Code Online (Sandbox Code Playgroud)

ystart并且yend是小刻度的y坐标,按每个条形的序列计算0.2.xstart是小刻度的x坐标.这里我假设条形宽度为0.5.如果宽度小于或大于则应更改坐标.xend计算假设刻度为0.1宽.

ystart<-c(seq(0.2,2.1,0.2),seq(0.2,3.5,0.2),seq(0.2,3.5,0.2),seq(0.2,1.5,0.2))
yend=ystart
xstart<-c(rep(0.75,floor(2.1/0.2)),rep(1.75,floor(3.5/0.2)),rep(2.75,floor(3.5/0.2)),rep(3.75,floor(1.5/0.2)))
xend<-xstart+0.1
Run Code Online (Sandbox Code Playgroud)

新值添加到数据框中.

myd <-data.frame(myd,ystart,yend,xstart,xend)

  p <- ggplot(myd, aes(factor(names), height,fill = names))
  p <- p + geom_bar(width=0.5)  

  #This line adds small ticks (segments) to bars
  p <- p + geom_segment(aes(x=xstart,y=ystart,xend=xend,yend=yend))

  #This line adds white lines at 1, 2 and 3
  p <- p + geom_hline(yintercept=c(1,2,3),color="white",lwd=1.1)

  #Next two lines removes legend and makes place for text
  p <- p + guides(fill=FALSE)
  p <- p + ylim(c(0,4))

  #Add numbers over bars
  p <- p + annotate("text",x=c(1,2,3,4),y=c(2.4,3.8,3.8,1.8),label=c("2.1","3.5","3.5","1.5"),angle=90,fontface="bold",size=5)

  #Adjustment of appearance to remove guidlines and axis ticks
  p <- p + theme_bw()
  p <- p + theme(axis.title=element_blank(),
          axis.text.y=element_blank(),
          axis.text.x=element_text(angle=90,face="bold",size=rel(1.5)),
          axis.ticks=element_blank(),
          panel.border=element_blank(),
          panel.grid=element_blank())
  print(p)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

编辑 - 添加解决方案作为功能.

制作函数ruler.func()- 只需要参数是条形高度的向量.函数的第一部分产生数据框,然后第二部分产生图.

ruler.func<-function(gg){
seq.list<-list()
for(i in 1:length(gg)){  
  ystart<-seq(0.2,gg[i],0.2)
  yend<-ystart
  xstart<-rep(i-0.25,length(ystart))
  xend<-xstart+0.1
  nam.val<-c(LETTERS[i],rep(NA,length(ystart)-1))
  numb.val<-c(gg[i],rep(NA,length(ystart)-1))
  seq.list[[i]]<-data.frame(nam.val,numb.val,xstart,xend,ystart,yend)
}
df<-as.data.frame(do.call(rbind, seq.list))
p <- ggplot(df, aes(nam.val))
p <- p + geom_bar(aes(y=numb.val,fill=nam.val),stat="identity",width=0.5,color="black",lwd=1.1)+
    scale_x_discrete(limits=LETTERS[1:length(gg)])+
    geom_segment(aes(x=xstart,y=ystart,xend=xend,yend=yend))+
    geom_hline(yintercept=seq(1,max(gg),1),color="white",lwd=1.1)+
    guides(fill=FALSE)+
    ylim(c(0,max(gg)+0.5))+
    annotate("text",x=seq(1,length(gg),1),y=gg+0.5,label=gg,angle=90,fontface="bold",size=rel(6))+
    theme_bw()+
    theme(axis.title=element_blank(),
               axis.text.y=element_blank(),
               axis.text.x=element_text(angle=90,face="bold",size=rel(1.5)),
               axis.ticks=element_blank(),
               panel.border=element_blank(),
               panel.grid=element_blank())
print(p)
}
Run Code Online (Sandbox Code Playgroud)

数字为1.2,4.6和2.8的示例.

ruler.func(c(1.2,4.6,2.8))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • @SHRram也添加了解决方案作为功能. (2认同)

ags*_*udy 5

我的方法是使用网格包自定义网格凹凸.它是后ggplot2处理的情节.我使用Grid低级函数通过向ggplot2图添加一些新图来进行自定义.这里有很大的优势,无需添加数据.您可以使用代码.

我认为ggplot2 +网格处理的混合是一个强大的工具来定制图.

#get the viewport (here we do the stuff)
library(grid)
library(plyr)
## 
grid.edit('geom_rect',gp=gpar(col='black'),grep=T)
## get the panel viewport
vp1 <- grid.get('panel',grep=T)$wrapvp
depth <- downViewport(name=vp1$name)
rects <- grid.get('geom_rect',grep=T)
for(i in 1:4){
  ## for each axis i create a view port , within it I draw my yaxis
  vpaxis <- viewport(x = rects$x[i]+rects$width[i],
                     y = rects$y[i], 
                     w = 0.005, 
                     h = rects$height[i],
                     just=c('left','top'),
                     yscale = c(0,myd$height[i])
  )
  ## I create the axis, you can customize it using the gp parameter
  gxa <- yaxisGrob(name='axiss',vp=vpaxis,
                   at = seq(0,myd$height[i],by=0.5),
                   gp=gpar(cex=.8))
  grid.draw(gxa)
}
## I put a blank background
grid.edit('background.rect',grep=T,gp=gpar(fill=NA))
###
upViewport(depth)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

对于坐标floop我们使用xaxis我们通过以下方式更改上面的for循环:

for(i in 1:4){
vpaxis <- viewport(x = rects$x[i],
                   y = rects$y[i], 
                   w = rects$width[i], 
                   h = 0,
                   just=c('left','top'),
                   clip=FALSE,
                   xscale = c(0,myd$height[i]),

)
gxa <- xaxisGrob(name='axiss',vp=vpaxis,
                 at = seq(0,myd$height[i],by=0.5),
                 gp=gpar(cex=.8))
grid.draw(gxa)
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述