在堆叠的格子条形图中显示值 [英] display values in stacked lattice barchart
问题描述
我想为它的每个部分显示100%栏的值。不幸的是我不知道该怎么做。由于图例的位置,图形应该位于格子中(我使用ggplot2进行了尝试,但无法在一行中显示图例)。我对任何建议或想法感到高兴。
$ b
library(lattice)
data(postdoc,package =latticeExtra)
colnames(postdoc) < -c(图例文字1,2,3,4,5)
色彩集合< - 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))
自定义面板函数:
$ b $ pre $ library $
library(plyr)
data(postdoc, (图例文字1,2,3,4,5)
colnames(postdoc)< - c rgb(166,27,30,maxColorValue = 255),
rgb(192,80,77,maxColorValue = 255),
rgb(24,65,83,maxColorValue = 255),
bgb(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 (...)
tmp< - list(...)
tmp< - data.frame(x = tmp $ x,y = tmp $ y)
#计算文本标签的位置
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))
格子图http://img819.imageshack.us/img819/894/ panelbarchart.png
I want to display the values of a 100% bar for each part of it. Unfortunately I don't know how to do it. The graph should be in lattice because of the legend position (I tried it with ggplot2, but you can't show the legend in one row). I'm pleased about any suggestions or ideas.
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))
This is achieved using a custom panel function:
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
这篇关于在堆叠的格子条形图中显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!