如何自动执行下降条上的数据标签?

时间:2019-07-06 14:44:53

标签: r plot

我想使条形图上的数据标签自动化。我知道当条形图不下降时如何自动化数据标签,这在上一个问题中得到了回答。我希望解决方案只能在基数R中。

我已经尝试将此代码添加到下面的代码中以使标签自动化,但是它不起作用。

 # labels #HERE THE LABEL CODE DOESNT WORK AFTER DESCENDING
 xFun <- function(x) x/2 + c(0, cumsum(x)[-length(x)])
 labs <- data.frame(x=as.vector(sapply(byb, xFun)),  # apply `xFun` here 
               y=rep(byb, each=nrow(byb)),  # use `byc` here
               labels=as.vector(apply(byb, 1:2, paste0, "%")), 
               stringsAsFactors=FALSE)
 labs$labels[labs$labels %in% paste0(0:(8*100)/100, "%")] <- "" #masks 
 labels <8

 invisible(sapply(seq(nrow(labs)), function(x)  # `invisible` prevents 
 unneeded console output
 text(x=labs[x, 1:2], labels=labs[x, 3], cex=.9, font=2, col=0)))


  initialmth <- structure(list( 
  A = c( 10, 4),
  B = c(28, 18),
  C = c(9, 1), 
  D = c(39, 33),
  E = c(13, 8),
  F = c(37, 27), 
  G = c(30, 51), 
  H = c(31, 41)),  


  .Names = c("Math has been my worst subject ",
         "I would consider a career that uses math ",
         "Math is hard for me",
         "I am the type of student to do well in math",
         "I cannot do a good job with math",
         "I could do advanced work in math",
         "I can get good grades in math",
         "I am good at math"
    ), 
   class = "data.frame", 
   row.names = c(NA, -2L)) #4L=number of numbers in each letter vector#

   attach(initialmth)
   print(initialmth)
   par(mar=c(0, 17, 1, 2.1))
   colors <- c("slategray3", "dodgerblue4")


   par(mar=c(0, 17, 1, 2.1))
   initialmth <- initialmth[, order(colSums(initialmth))]
   colors <- c("slategray3", "dodgerblue4")
   byb <- barplot(as.matrix(initialmth), 
           beside = F, 
           horiz = T, col=colors,  main="N=96", xaxt="n", border=F, las=1)

1 个答案:

答案 0 :(得分:2)

有关说明,请参见前面的thisthis

initialmth <- initialmth[, order(colSums(initialmth))]

xFun <- function(x) x/2 + c(0, cumsum(x)[-length(x)])

par(mar=c(0, 17, 1, 2.1))
colors <- c("slategray3", "dodgerblue4")
byc <- barplot(as.matrix(initialmth), 
               beside = F, ylim = range(0, 15), xlim = range(0, 100),
               horiz = T, col=colors,  main="N=96", xaxt="n", border=F, las=1, width 
               = 1.45)
# labels
labs <- data.frame(x=as.vector(sapply(initialmth, xFun)),  # apply `xFun` here 
                   y=rep(byc, each=nrow(initialmth)),  # use `byc` here
                   labels=as.vector(apply(initialmth, 1:2, paste0, "%")), 
                   stringsAsFactors=FALSE)
labs$labels[labs$labels %in% paste0(0:(8*100)/100, "%")] <- "" #masks labels <8

invisible(sapply(seq(nrow(labs)), function(x)  # `invisible` prevents unneeded console output
  text(x=labs[x, 1:2], labels=labs[x, 3], cex=.9, font=2, col=0)))

enter image description here

数据

initialmth <- structure(list(`Math has been my worst subject ` = c(10, 4), 
    `I would consider a career that uses math ` = c(28, 18), 
    `Math is hard for me` = c(9, 1), `I am the type of student to do well in math` = c(39, 
    33), `I cannot do a good job with math` = c(13, 8), `I could do advanced work in math` = c(37, 
    27), `I can get good grades in math` = c(30, 51), `I am good at math` = c(31, 
    41)), class = "data.frame", row.names = c(NA, -2L))