T.T
T.T

Reputation: 27

Issue about the scale of y-axis in a bar chart in r

This image is the dataset that I read into R, and I call it dat3

I tried to use "ggplot" to create a bar chart to summary this dataset. Here are my codes:

ggplot(dat3,aes(variable,value,fill=Industries))+geom_bar(stat="identity",position=
"dodge")+scale_fill_brewer(palette = "Set1")

The bar chart look like: enter image description here

As we can see, the scale of value looks messy, it doesn't follow the order. How can I fix it?

Upvotes: 0

Views: 400

Answers (1)

neilfws
neilfws

Reputation: 33782

The main problem here is that the value column is not numeric. It contains commas which would make it a character variable - but unless you specify stringsAsFactors = FALSE when you import the data, the values are converted to a factor (as you can see by the <fctr> at the top of the column).

The quickest fix is to convert that column to character, remove the commas, then convert to numeric:

dat3$value <- as.character(dat3$value)
dat3$value <- gsub(",", "", dat3$value)
dat3$value <- as.numeric(dat3$value)

You should also decide whether you want Industries and variable to be factors.

Upvotes: 2

Related Questions