Home > OS >  How to change with for bar chart that display only 2 items?
How to change with for bar chart that display only 2 items?

Time:12-03

I wanted display 2 items on a bar chart. I have used the width argument to reduce the width of each bar but they still do not get adjusted. I found that by increasing the space argument the bars will get smaller, but they will be very far apart. Could anyone please help explain to me how to fix this problem? I have attached the codes and chart below for your reference. Thank you for your time.

predErr<-c(mars = MARS_Predictions_Err, svm = SVM_Predictions_Err)
barplot(predErr, main = "Prediction Error Bar Chart", xlab = "Models", ylab = "Err", width = 1, ylim = c(0,1000)) 

enter image description here

CodePudding user response:

You have to play around with three parameters: width (sets the width of each bar), xlim (sets the range of the x-axis) and space (sets the distance between bars as a fraction of the average bar width).

For example, this parameter combination:

heights = c("m1"=2, "m2"=4)
barplot(heights, 
        xlab = "Models", 
        ylab = "Err", 
        width = c(0.1,0.1), xlim = c(0,1), space = 3)

gave me this result, which I believe is what you want:

enter image description here

CodePudding user response:

I generated some data. To change the distance between the bars, I used space=1 . Is this your response to your question?

set.seed(100)

MARS_Predictions_Err<-rnorm(1,200)
SVM_Predictions_Err<-rnorm(1,200)

predErr<-c(mars = MARS_Predictions_Err, svm = SVM_Predictions_Err)
barplot(predErr, main = "Prediction Error Bar Chart", 
        xlab = "Models", 
        ylab = "Err", width = 3, 
        ylim = c(0,1000),
        space=1)

space=1

enter image description here

space=0 enter image description here

  • Related