简体   繁体   English

R-Project Barplot颜色

[英]R-Project Barplot Colors

I am new to R, and I am trying to create a simple barplot. 我是R的新手,我正在尝试创建一个简单的条形图。 I have been able to create a barplot with the correct values, but only a single color for all bars. 我已经能够创建一个具有正确值的条形图,但所有条形图只能使用一种颜色。 If I change the code slightly ( using table() instead of as.table() ), I get the wrong values, but the correct colors on the graph. 如果我稍微更改代码(使用table()而不是as.table()),我会得到错误的值,但图表上的颜色正确。 How can I get the as.table() to accept multiple colors in a graph? 如何让as.table()接受图形中的多种颜色? Here is an altered version of my code: 这是我的代码的更改版本:

a=30
b=20
c=10
d=15
x=matrix(c(a,b,c,d),ncol=4,byrow=TRUE)   
colnames(x)=c("Label1","Label2","Label3","Label4")  
    rownames(x)=c("Percentage")  
    x=as.table(x)  
    color=c("red","blue","green","orange")
barplot(x,main="X",ylab="Percent",cex.names=0.75,col=color)

Use the beside = TRUE argument: 使用beside = TRUE参数:

barplot(x, beside = TRUE, main="X", ylab="Percent", cex.names=0.75, col=color)

在此输入图像描述

Passing in a vector instead of a table should do the trick: 传入向量而不是表应该可以解决问题:

barplot(x[1, ], main="X", ylab="Percent", cex.names=0.75, col=color)

Or 要么

barplot(x["Percentage", ], main="X",ylab="Percent",cex.names=0.75, col=color)

在此输入图像描述

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

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