R语言 如何在堆叠直方图上添加和自动化标签

e3bfsja2  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(93)

我想给我的直方图添加标签。我一开始是水平的,但现在想让它垂直。我不知道如何修改代码,以便显示年份并自动添加数据标签。

counts<- structure(list( 

 A = c(201,70), 
 B = c(191,66),
 C =  c(197,69),
 D = c(206,73),
 E = c(207,73), 
 F = c(212,75),
 G = c(207,70)),

 .Names = c("2016",
         "2017",
         "2018",
         "2019",
         "2020",
         "2021",
         "2022"
         
  ),

 class = "data.frame", 
 row.names = c(NA, -2L)) #4L=number of numbers in each letter vector#

 attach(counts)
 print(counts)

# barplot
colors <- c("paleturquoise3","sienna3")
xFun <- function(x) x/2 + c(0, cumsum(x)[-length(x)])
par(mar=c(2, 4, 7, 2) + 0.1)  # this sets margins to allow long labels
byc <- barplot(as.matrix(counts), horiz=F, col=colors,
           # assign `byc`
           border=FALSE, las=1, xaxt='n',main="Mean Time Loss by Companies due to      Data Breaches",
           ylim = range(0, 300), xlim = range(0, 8),
           width = 1)

 # labels
 labs <- data.frame(x=as.vector(sapply(counts, xFun)),  # apply `xFun` here 
               y=rep(byc, each=nrow(counts)),  # use `byc` here
               labels=as.vector(apply(counts, 1:2, paste0)), 
               stringsAsFactors=FALSE)
 labs$labels[labs$paste0(0:(8*100)/100 "")] <- "" 

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

 # legend  (set `xpd=TRUE` to plot beyond margins!)
 legend(0, 360.25,  legend=c("Mean time to identify (days)","Mean time to contain      (days)"),                                                                                                                                                                                                                                                                
   fill=colors, horiz = T, bty='n', xpd=T, border = F)

字符串

xiozqbni

xiozqbni1#

你在找这样的东西吗:

par(mar=c(2, 4, 7, 2) + 0.1)  # this sets margins to allow long labels
byc <- barplot(as.matrix(counts), horiz=F, col=colors,
               # assign `byc`
               border=FALSE, las=1, yaxt='n', main="Mean Time Loss by Companies due to      Data Breaches",
)
# labels
labs <- data.frame(x=as.vector(sapply(counts, xFun)),  # apply `xFun` here 
                   y=rep(byc, each=nrow(counts)),  # use `byc` here
                   labels=as.vector(apply(counts, 1:2, paste0)), 
                   stringsAsFactors=FALSE)

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

# legend  (set `xpd=TRUE` to plot beyond margins!)
legend(0, 330.25,  legend=c("Mean time to identify (days)","Mean time to contain      (days)"),                                                                                                                                                                                                                                                                
       fill=colors, horiz = T, bty='n', xpd=T, border = F)

字符串


的数据

相关问题