在格子条形图中添加线条和标签[R]

时间:2010-07-12 20:08:40

标签: graphics r lattice

我有两个关于在格子中定制条形图的问题。我试图在条形图上添加标签分别编辑比例,但我的代码不起作用。我做错了什么?应该有一些值,包括“%”。

第二个问题是如何在条形后面的绘图区域的顶部和底部之间绘制线条,并且位于打印的刻度值。谢谢你的任何建议和帮助,塞巴斯蒂安。

以下是代码:

library(lattice)
library(plyr)
data(postdoc, package="latticeExtra")
colnames(postdoc) <- c("Legendtext 1", "2", "3", "4", "5")
colors <- c(rgb(166,27,30,maxColorValue = 255),                             
        rgb(192,80,77,maxColorValue = 255),
        rgb(24,65,83,maxColorValue = 255),
        rgb(60,143,167,maxColorValue = 255),
        rgb(130,184,208,maxColorValue = 255))
colorset <- simpleTheme(col=colors,                                          
                    border="white")
data.p <- prop.table(postdoc, margin=1)
data <- data.p * 100
pl <- barchart(data,
           par.settings = colorset,                                      
           box.ratio = 1,                                                
           xlab = " ",                                                   
           panel = function(...) {   # Code for panel function by rcs (http://stackoverflow.com/questions/3220702/display-values-in-stacked-lattice-barchart-r) - thank you very much!
             panel.barchart(...) 
             tmp <- list(...)
             tmp <- data.frame(x=tmp$x, y=tmp$y)
             # calculate positions of text labels                        
             df <- ddply(tmp, .(y),
                         function(x) {
                           data.frame(x, pos=cumsum(x$x)-x$x/2)
                         })
             panel.text(x=df$pos, y=df$y,                                
                        label=sprintf("%.0f", df$x),                     
                        cex=0.7, col="white")
             panel.axis(side = c("bottom"),
                        at = list(0, 20, 50, 75, 100),
                        labels = list("0%", "25%", "50%", "75%", "100%"),
                        ticks = TRUE)       
           },
           auto.key=list(columns=5, space="bottom",                          
                         cex=0.8, size=1.4, adj=1,
                         between=0.2, between.colums=0.1, 
                         size = 1.3, points = FALSE, rectangles = TRUE))

1 个答案:

答案 0 :(得分:3)

问题1:以下内容适用于我(half=FALSE

panel.axis(side=c("bottom"),
           at=c(0, 25, 50, 75, 100),
           labels=c("0%", "25%", "50%", "75%", "100%"),
           rot=0, half=FALSE, ticks=TRUE)

问题2:在面板功能panel.grid之前添加对panel.abline(或panel.barchart())的来电:

panel.grid(h=FALSE, v=-1)