Как автоматизировать метки данных на нисходящих столбцах? - PullRequest
0 голосов
/ 06 июля 2019

Я хочу автоматизировать метки данных на нисходящей гистограмме.Я знаю, как автоматизировать метки данных, когда столбцы не опускаются, на что был дан ответ в предыдущем вопросе.Мне бы хотелось, чтобы решение было только в базе 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 Ответ

2 голосов
/ 06 июля 2019

Объяснения см. В этом и этом предыдущих ответах.

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))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...