在点阵条形图中添加线条和标签 [R]

发布于 2024-09-08 19:51:40 字数 2236 浏览 9 评论 0原文

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

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

这是代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

复古式 2024-09-15 19:51:40

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

Question 1: The following works for me (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)

Question 2: Add a call to panel.grid (or panel.abline) before panel.barchart() in your panel function:

panel.grid(h=FALSE, v=-1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文