【问题标题】:display values in stacked lattice barchart在堆叠格子条形图中显示值
【发布时间】:2011-03-14 08:06:28
【问题描述】:

我想为它的每个部分显示一个 100% 条的值。不幸的是,我不知道该怎么做。由于图例的位置,该图应该是格子的(我用 ggplot2 试过,但你不能在一行中显示图例)。我很高兴有任何建议或想法。

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

【问题讨论】:

    标签: graphics r lattice


    【解决方案1】:

    这是使用自定义面板功能实现的:

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

    【讨论】:

    • 非常感谢!这对我帮助很大!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-03
    • 2020-10-05
    • 2021-09-07
    • 2023-03-25
    • 2018-02-20
    • 2014-08-24
    相关资源
    最近更新 更多