Reputation: 2011
I have a dataset as follows: (10,75) (20,80) (50,85) (100,92)
How to plot a bar-graph in R? I saw many examples in the net but none of them conform to this simple circumstance. Thanks
Upvotes: 0
Views: 170
Reputation: 4385
As an alternative, you can always use the ggplot2
library. Because of the way the data is shaped, you should also use the reshape2
library to differentiate between variables. It's a bit more complicated in this case, but in general you'll get nicer-looking barplots.
library(ggplot2)
library(reshape2)
#id variable tells what row number is used
data1=as.data.frame(cbind(id=1:4,var1=c(10,20,50,100),var2=c(75,80,85,92)))
#melt will create a row for each variable of each row, except it saves the id as a separate variable that's on every row
data1=melt(data1,id.vars='id')
#ggplot tells what data set is used and which variables do what
#geom_bar tells what type of plot should be used and certain options for the plot
ggplot(data1,aes(x=id,y=value,fill=variable))+geom_bar(stat='identity',position='dodge')
Upvotes: 1
Reputation: 1932
Try this:
data1=rbind(c(10,20,50,100),c(75,80,85,92))
barplot(data1, beside=TRUE, col=c("blue", "red"))
Upvotes: 2