R选择性地绘制曲线轴标签

时间:2013-12-01 03:11:04

标签: r

我目前有以下R代码,并且想知道是否可以选择性地粗体/斜体/应用其他样式轴上的标签

> w1 <- read.csv(file="test1.csv",sep=",", head=TRUE)
> w1$Commits <- w1$normal + w1$merges
> w1
        Commits normal merges
John          4      3      1
Sarah        19     13      6
Zach          3      3      0
Jacob        27     24      3
> titles <- w1$merges / w1$Commits
> titles <- sprintf("%s%%", format(round(titles * 100), 1))
> bp <- barplot(rbind(w1$normal, w1$merges), main="Playing with R", xlab="Who", names.arg=rownames(w1), col=c("blue", "red"), ylim = c(0,30))
> text(bp, rbind(w1$Commits), labels = titles, pos = 3, cex = 0.75)

这会产生一个漂亮的条形图(我可能已经使用了%d而不是格式,但这并不重要),正是我想要的,但是我也希望能够将names.arg列表设置为样式,以便在我可以在底部看到标签:

John Sarah Zach Jacob

有没有办法将样式应用于这些标签?或者用其他方式将某些条形标记为不同(灰色背景等)?

2 个答案:

答案 0 :(得分:2)

以下是使用bquote(和do.call

的方法
 # the order in which you want the "bolding"
 xx <- expression(bold(.(x)), italic(.(x)), .(x), .(x))
 # a list of lists containing the information for bquote
 xL <- lapply(rownames(w1), function(x) list(x=x))
 # using Map and do.call and as.expression to create the list of expressions
 labs <- as.expression(Map(function(expr,where) {do.call(bquote, list(expr,where))},
   expr = xx, where =xL))

bp <- barplot(rbind(w1$normal, w1$merges), 
      main="Playing with R", xlab="Who", names.arg=rep("",4), 
      col=c("blue", "red"), ylim = c(0,30))
text(bp, rbind(w1$Commits), labels = titles, pos = 3, cex = 0.75)
axis(1, at=bp, labels= labs)

或类似地使用substitute

xs <- expression(bold(x), italic(x), x, x)
labs <- Map(function(expr,where) {do.call(substitute, list(expr,where))}, expr = xs, where =xL)

答案 1 :(得分:1)

bp <- barplot(rbind(w1$normal, w1$merges), 
          main="Playing with R", xlab="Who", names.arg=rep("",4), 
          col=c("blue", "red"), ylim = c(0,30))
text(bp, rbind(w1$Commits), labels = titles, pos = 3, cex = 0.75)
axis(1, at=bp, labels= parse(text= 
                     gsub("\\s", '~', paste0(c("bold(", "", "italic(", ""),  
                                             rownames(w1), 
                                             c(    ")", "",       ")", "")
                          )                 )
             )          )

enter image description here 这看起来很笨拙,但你应该看到我用expressionbquotesubstitute制作长度为4的表达向量的失败努力。