简体   繁体   中英

R-Project Barplot Colors

I am new to R, and I am trying to create a simple barplot. 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. How can I get the as.table() to accept multiple colors in a graph? 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:

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)

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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