在条形图中为面板添加不同的文本

时间:2014-10-31 15:21:28

标签: r text bar-chart lattice

您好我想在barchart的两个面板中添加不同的文字。这两个文本值表示barchart中0值的百分比。我一直在寻找不同的帮助网站,但还没有找到答案。这是我的尝试:

数据

dput(ztable)
structure(list(Group.1 = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 11L, 12L, 14L, 17L, 19L, 29L, 115L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 9L, 11L, 12L, 14L, 17L, 19L, 29L, 115L), .Label = c("0", 
"10", "20", "30", "40", "50", "60", "70", "80", "90", "100", 
"110", "120", "130", "140", "150", "160", "170", "180", "190", 
"200", "210", "220", "230", "240", "250", "260", "270", "280", 
"290", "300", "310", "320", "330", "340", "350", "360", "370", 
"380", "390", "400", "410", "420", "430", "440", "450", "460", 
"470", "480", "490", "500", "510", "520", "530", "540", "550", 
"560", "570", "580", "590", "600", "610", "620", "630", "640", 
"650", "660", "670", "680", "690", "700", "710", "720", "730", 
"740", "750", "760", "770", "780", "790", "800", "810", "820", 
"830", "840", "850", "860", "870", "880", "890", "900", "910", 
"920", "930", "940", "950", "960", "970", "980", "990", "1000", 
"1010", "1020", "1030", "1040", "1050", "1060", "1070", "1080", 
"1090", "1100", "1110", "1120", "1130", "1140"), class = "factor"), 
    Group.2 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Fall", "Spring"
    ), class = "factor"), x = c(677L, 344L, 18L, 6L, 5L, 4L, 
    0L, 2L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 82L, 186L, 15L, 6L, 
    8L, 1L, 3L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 0L)), .Names = c("Group.1", 
"Group.2", "x"), row.names = c(NA, -32L), class = "data.frame")

条形图代码

mytext<-c("63.9","7.7")

barchart(ztable[,3]~ztable[,1]|ztable[,2],col="light grey", 
    scales=list(y=list(cex=.8),x=list(cex=.8,rot=90)),ylim=c(0,800),
    xlab="Number of Black Sea Bass",ylab="Number of Stations",
    panel=function(x,y,...){
    panel.barchart(x,y,...)
    panel.text(1,0,labels=mytext[panel.number()]) }
)   

1 个答案:

答案 0 :(得分:0)

您的panel.text()功能中只有一个小错误。 x和y坐标使它无法读取文本。 barchart中的x坐标(这是panel.text函数中的第一个数字)是指条形码。你有1,大约8把它放在中间。 y坐标是指y轴上的值。这是panel.text' function. It is currently 0, but 400 works better. So replace your panel.text`行中的第二个数字:

panel.text(8,400,labels=mytext[panel.number()]) }

你得到:

enter image description here