簡體   English   中英

R圖形:向堆積條形圖添加標簽

[英]R graphics: Add labels to stacked bar chart

我正在尋找一種方法,使用R的基本繪圖功能將標簽(即絕對值)添加到堆積條形圖中。標簽應位於堆疊條形圖內。

謝謝!

barplot將返回barplot的中間x位置,所以你可以這樣做

mydata <- matrix(c(10, 21, 22, 33, 45, 23, 22, 43, 33), nrow=3)

# b will contain the x midpoints of the bars
b <- barplot(mydata)

# This will write labels in the middle of the bars, horizontally and vertically
text(b, colMeans(mydata), c("Label1", "Label2", "Label3"))

# This will write labels in the middle of the middle block
text(b, mydata[1,]+mydata[2,]/2, c("LabelA", "LabelB", "LabelC"))

編輯:重新閱讀你的問題,我認為這是你想要的(或者可能不是,但我還是會寫的:D)

# Find the top y position of each block 
ypos <- apply(mydata, 2, cumsum)
# Move it downwards half the size of each block
ypos <- ypos - mydata/2
ypos <- t(ypos)

text(b, ypos, mydata)

簡單的函數text()怎么樣?

您可以隨意添加字符串,例如:

text (x = ..., y = ..., labels = c("foo bar 1000"))

也許你可以使用或檢查plotrix包的barp功能

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM