-1

I am trying to make a bar chart to show my dataset. There is a issue about the scale of the bar chart, here is my plot:

enter image description here

Here is part of my data Data I don't know why the biggest number is on the bottom and the small number is on the top. How can I fix it? Here are the codes:

library(ggplot2)
ggplot(dat1) + geom_bar(aes(dat1$X...Country,dat1$X2017),stat = "identity")
zx8754
  • 52,746
  • 12
  • 114
  • 209
T.T
  • 27
  • 1
  • 4
  • 4
    Look like your data is stored as character/factors rather than numbers. You should supply a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data so we can confirm. – MrFlick Oct 20 '17 at 18:26
  • could you please provide part of your data. –  Oct 20 '17 at 19:26

1 Answers1

1

Your X2007 and X2008 columns are stored as factors, but you probably want them to be numeric. If you coerce a factor to a numeric, you will probably get unexpected results. So I'd recommend coercing to character and then to numeric. The commas present a problem there, so after converting to character, we'll remove the commas, then convert to numeric.

dat1$X2007 <- as.numeric(gsub(",", "", as.character(dat1$X2007)))

I got this trick from a previous post: How to read data when some numbers contain commas as thousand separator?

Alex P
  • 1,574
  • 13
  • 28
  • I followed you suggestion, but it still doesn't work. The result said :"Removed 5 rows containing missing values (position_stack)." – T.T Oct 20 '17 at 20:43
  • Ok, I think I may have figured out what happened. Does my edited answer work? – Alex P Oct 22 '17 at 00:09