繁体   English   中英

为图例添加颜色

[英]Add color to legend

我创建了以下barplot ,但问题是图例没有正确的颜色,并且网格位于条形图的前面。 我想在图例的方框中放置正确的列,并将网格放在条形后面。

我还想在每个栏的底部打勾以识别它们

我怎样才能在R中执行这些功能?

barkplot

我使用的r脚本:

autos_data <- read.table("data.dat", header=T, sep="\t") 

barplot(rep(NA,length(autos_data)),ylim=c(0,max(autos_data)),axes=FALSE)
barplot(t(as.matrix(autos_data)), main=NULL, ylab= "% of Cloud usage", xlab="Input data size (MB)", ylim=c(0,50), beside=TRUE, lwd=1:2, angle=c(45,135), density=seq(5,40,10), col=c("blue","red","black","darkgreen"))
grid(NA, 5, lwd=1,lty=5, col="gray") # grid only in y-direction  

# Place the legend at the top-left corner with no frame
coords="top"
legend(coords, c("WestVirginia","Chicago I","Chicago II","California"), text.col=par("col"), cex=1.2, bty="n", xpd=TRUE, horiz=TRUE, inset=c(0,-.22), angle=c(35,135,45,135), density=seq(5,40,10),col=c("blue","red","black","darkgreen"))

编辑:

dput(autos_data)
structure(list(WestVirginia = c(29L, 29L, 23L, 23L), ChicagoI = c(30L, 
21L, 36L, 26L), ChicagoII = c(39L, 42L, 35L, 46L), California = c(2L, 
8L, 6L, 5L)), .Names = c("WestVirginia", "ChicagoI", "ChicagoII", 
"California"), class = "data.frame", row.names = c("1500", "3000", 
"4500", "6000"))

数据:

West-Virginia   ChicagoI    ChicagoII   California
1500    29  30  39  2
3000    29  21  42  8
4500    23  36  35  6
6000    23  26  46  5

这是参数名称的不一致。

你需要设置fill

legend(coords, c("WestVirginia","Chicago I","Chicago II","California"), text.col=par("col"), 
       cex=1.2, bty="n", xpd=TRUE, horiz=TRUE, inset=c(0,-.22), angle=c(35,135,45,135), 
       density=seq(5,40,10), fill=c("blue","red","black","darkgreen"))

在此输入图像描述

您可以直接计算出传说中的barplot调用,通过指定的参数args.legend参数,并通过文本legend.text 对于网格,您需要在设置网格后重新barplot ,使用白色然后是线条,以便网格不再出现在线条中:

它需要3个相同的barplot调用,但我认为它可以满足您的需求:

# first plot
barplot(t(as.matrix(autos_data)), main=NULL, axes=FALSE, beside=TRUE, col="white", ylab= "% of Cloud usage", xlab="Input data size (MB)", ylim=c(0,50))
# add the grid
grid(NA, 5, lwd=1,lty=5, col="gray") # grid only in y-direction  
# hide the grid with white bars
barplot(t(as.matrix(autos_data)), main=NULL, beside=TRUE, col="white", ylab= "% of Cloud usage", xlab="Input data size (MB)", ylim=c(0,50), add=TRUE)
# plot the density lines, the legend, etc.
barplot(t(as.matrix(autos_data)), main=NULL, axes=FALSE, beside=TRUE, 
        lwd=1:2, angle=c(45,135), density=seq(5,40,10), col=c("blue","red","black","darkgreen"), 
        legend.text=c("WestVirginia","Chicago I","Chicago II","California"), 
        args.legend=list(x="top", y=NULL, bty="n", ncol=4), add=TRUE)

在此输入图像描述

在你的legend设置fill而不是col

legend(
  coords, 
  legend = c("WestVirginia","Chicago I","Chicago II","California"), 
  text.col=par("col"), 
  cex=1.2, 
  bty="n", 
  xpd=TRUE,
  horiz=TRUE, 
  inset=c(0,-.22), 
  angle=c(35,135,45,135), 
  density=seq(5,40,10),
  fill=c("blue","red","black","darkgreen")
) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM