在点阵条形图中添加线条和标签 [R]
我有两个关于在点阵中自定义条形图的问题。我尝试向条形图添加标签,分别编辑比例,但我的代码不起作用。我做错了什么?应该有一些值,包括“%”。
第二个问题是如何在条形后面的绘图区域的顶部和底部之间绘制线条并位于打印的比例值处。感谢您的任何建议和帮助,塞巴斯蒂安。
这是代码:
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))
I have two questions concerning customizing a barchart in lattice. I tried to add labels to the barchart respectively edit the scale, but my code doesn't work. What I'm doing wrong? There should be some values including "%".
The second question is how lines can be drawn between the top and the bottom of the plotting area behind the bars and located at the printed scale values. Thank you for any suggestions and help, Sebastian.
Here is the code:
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))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题 1:以下内容对我有用 (
half=FALSE
)问题 2:添加对
panel.grid
(或panel.abline
)的调用在面板函数中的panel.barchart()
之前:Question 1: The following works for me (
half=FALSE
)Question 2: Add a call to
panel.grid
(orpanel.abline
) beforepanel.barchart()
in your panel function: