2010-07-10 37 views
10

Tôi muốn hiển thị các giá trị của thanh 100% cho mỗi phần của nó. Thật không may tôi không biết làm thế nào để làm điều đó. Biểu đồ phải ở trong mạng vì vị trí chú giải (tôi đã thử nó với ggplot2, nhưng bạn không thể hiển thị chú thích trong một hàng). Tôi hài lòng về bất kỳ đề xuất hoặc ý tưởng nào.giá trị hiển thị trong thanh lưới xếp chồng lên nhau

library(lattice) 
data(postdoc, package = "latticeExtra") 
colnames(postdoc) <- c("Legendtext 1", "2", "3", "4", "5") 
colorset <- simpleTheme(col = 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)), 
           border = "white") 
pl <- barchart(prop.table(postdoc, margin = 1), 
       par.settings = colorset, 
       auto.key = list(columns = 5, space = "bottom", 
           cex = 0.8, size = 1.4, between = 0.2, 
           between.columns = 0.1, adj = 1)) 

Trả lời

18

Điều này đạt được bằng cách sử dụng chức năng bảng điều khiển tùy chỉnh:

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") 

pl <- barchart(prop.table(postdoc, margin=1), 
       par.settings=colorset, 
       panel=function(...) { 
       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("%.02f", df$x), 
          cex=0.7) 
       }, 
       auto.key=list(columns=5, space="bottom", 
          cex=0.8, size=1.4, adj=1, 
          between=0.2, between.colums=0.1)) 

lattice plot http://img819.imageshack.us/img819/894/panelbarchart.png

+0

Cảm ơn bạn rất nhiều! Đó là một trợ giúp tuyệt vời cho tôi! – SebastianW

Các vấn đề liên quan