在条形图的右侧添加文本

时间:2015-12-04 20:31:13

标签: r text plot

这是我目前的代码:

vec2<- c(10.810811, 18.146718, 25.096525, 26.640927, 16.216216,  3.088803)
b<-barplot(as.matrix(vec2), ylab = "Total Percentage", xlab="studies", col = c("red", "yellow", "green", "violet", "skyblue", "cyan"), width=1)
# Find the top y position of each block
ypos <- apply(as.matrix(vec2), 2, cumsum)
# Move it downwards half the size of each block
ypos1 <- ypos - vec2/2
ypos1 <- t(ypos1)
text(b, ypos1, labels=round(vec2))

使用我当前的代码如何在绘图的颜色编码部分右侧的右侧条形图中添加文本?我想要在情节的右边中间和外面的文字?另外我如何让我的条纹更瘦?

2 个答案:

答案 0 :(得分:2)

我最近处理过类似的情况。我认为我的解决方案也适用于您的情况。我通过使用layout()函数创建了一个多窗格图来完成此操作,其中legend()放置在一个狭窄的空窗格中。

我使用的代码的重要部分如下

#####################
# Set up the plots

BARPLOT_COLORS<-c("blue","purple","red")

# setup the matrix for the plot layout
# it is easier to create this in a csv file in Excel, etc., then read it into R as a matrix
# here is the dput() code for the matrix I made
Counts_matrix<-structure(c(1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 1L, 2L, 
                                     2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 
                                     3L, 3L, 3L, 3L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L),
                                   .Dim = c(11L,4L), 
                                   .Dimnames = list(NULL, c("V1", "V2", "V3", "V4")))
# looks like this
# > Counts_matrix
# V1 V2 V3 V4
# [1,]  1  1  1  1
# [2,]  2  2  2  2
# [3,]  2  2  2  2
# [4,]  2  2  2  2
# [5,]  2  2  2  2
# [6,]  2  2  2  2
# [7,]  3  3  3  3
# [8,]  3  3  3  3
# [9,]  3  3  3  3
# [10,]  3  3  3  3
# [11,]  3  3  3  3
# the numbers here correspond to contiguous areas in which the plots will be placed
# in the numerical order specified
# since the OP wants the legend on the right side, make a matrix like this with 1's along that side 


# setup the panel layout by passing the matrix to layout()
layout(Counts_matrix)
# layout.show(max(Counts_matrix))  # use this to preview the layout

par(mar=c(0,0,4,0)) # need to set this for some reason
# on mar: A numerical vector of the form c(bottom, left, top, right) which gives the number of lines of margin to be specified on the four sides of the plot. 
# The default is c(5, 4, 4, 2) + 0.1.

# call blank plot to fill the first panel
plot(1,type='n',axes=FALSE,xlab="",ylab="",main = "Quality Counts",cex.main=2) 

# set up the Legend in the first panel
legend("bottom",legend=c("Good","Bad","Ugly"),fill=BARPLOT_COLORS,bty = "n",ncol=length(BARPLOT_COLORS),cex=1.0)

# set the mar for the remaining panels
par(mar=c(6,5,0,4))
# create barplot for the two matrices
barplot(Raw_Counts,horiz = T,col=BARPLOT_COLORS,border=NA,las=1,cex.names=1,xlab="Number of counts (millions)")
barplot(Pcnt_Counts,horiz = T,col=BARPLOT_COLORS,border=NA,las=1,cex.names=1,xlab="Percent of counts")

我做了大量的阅读以弄清楚这个;可在此处找到一些相关链接:

http://seananderson.ca/courses/11-multipanel/multipanel.pdf

Common legend for multiple plots in R

我使用的整个代码可以在这里找到:

https://github.com/stevekm/Bioinformatics/blob/master/scripts/alignment_summary_muti_panel_barplot.R

你应该能够将其复制/粘贴到RStudio中并运行它以查看它是如何工作的

答案 1 :(得分:1)

如果您不想安装任何软件包,可以执行以下操作...

text(x = 1.25,y = 50,labels =&#34; Text Goes Here&#34;,srt = 270,xpd = T)

x确定x中的偏移量 y确定y的偏移量 标签是您希望它显示的标签 srt设置一个应用于它的角度(90将翻转我发布的内容)
xpd允许文本扩展到边界(尝试为False以查看我正在谈论的内容)