期待绘制分组的条形图
数据:
结构(主要= c(0.468893939007605,0.0629924918425918,0.561410474480681),总数= c(0.388090040532888,-0.0706047151157143,0.483298239353565)),…
这是因为 barplot 喜欢转置矩阵。
barplot
m <- as.matrix(main_total)
使用 t 转置矩阵。
t
b <- barplot(t(m), beside=TRUE, ylab="Value", ylim=c(round(min(m), 1), round(max(m), 1)), col=3:4) axis(1, colMeans(b), c("H", "M", "S")) legend("topleft", legend=c("Main", "Total"), fill=3:4) box()
的 给 强>
您可以在这里得到这个想法,因为您没有在示例代码中包含分组变量。
df <- structure(list(Main = c(0.468893939007605, 0.0629924918425918, 0.561410474480681), Total = c(0.388090040532888, -0.0706047151157143, 0.483298239353565)), class = "data.frame", row.names = c(NA, -3L)) df$Group <- c('H','M','S') # Assign group variables library(reshape2) # Data frame needs to be in long format df.m <- melt(df,id.vars = "Group") df.m library(ggplot2) ggplot(df.m, aes(x=Group,y=value)) + geom_bar(aes(fill=variable),stat = 'identity',position = 'dodge')