Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 205372

How do I create a geom_bar plot that has numeric variable values but where the scale isn't continuous?

$
0
0

I am trying to create a bar plot but I am having troubles with the fact that my x axis variable (forest tree species) are in the form of numerical codes. That makes it seem as if it is a continuous variable, but in fact the numbers are codes for categorical variables (2 means a certain tree species). I want to display my bar graph without the large gaps in between the bars, as there really is no data there... the categories that occupy those codes don't exist. Rather, I seek an evenly spaced graph where the variables are simply "names" (even though they are numbers).

Here is a mini code to illustrate:

library(tibble)
library(ggplot2)

canada <- tibble(
  ghousecode = c(100, 101, 102, 102, 102, 104, 107, 110, 110),
  product_code = c(2, 2, 2, 8, 21, 21, 36, 103, 150), 
)
ggplot(data = canada) +
  geom_bar(mapping = aes(x= product_code))

My graph comes out like this:

enter image description here

I'd like it so that all my bars were touching, and each bar is just labelled with its appropriate code.

Any help greatly appreciated. I have thought about perhaps converting the codes to the names to solve the issue, but in reality my data is 57,000 lines and I feel there must be a faster/better way to solve this.


Viewing all articles
Browse latest Browse all 205372

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>