0

I've run the following code and get the graph as provided here. Since the x-axis represents sampling locations I want to the gaps on the x-axis representing values which are irrelevant

ggplot(dat, aes(x=SU,fill=factor(SCIENTIFIC_NAME)))+
  geom_bar(width=0.5)+xlab("Sampling Unit (SU)")+
  ylab("Count")+labs(fill="SCIENTIFIC NAME")+
  ggtitle("Inventory per Sampling Unit (SU) by species")+ 
  scale_x_continuous(breaks=c(1,2,3,4,5,6,7,8,10,11,15))

Inventory count per sampling unit based on species

Waldi
  • 39,242
  • 6
  • 30
  • 78
  • Try to make your question [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), it's going to be easier to help you. – s__ Jun 23 '20 at 12:17
  • can you please give input data to the plot by copying to you post the result of dput(dat) – Waldi Jun 23 '20 at 12:18
  • 1
    Take a look at this [post](https://stackoverflow.com/questions/28742870/use-of-scale-x-discrete-in-r-ggplot2) – xilliam Jun 23 '20 at 12:19
  • 1
    This is a guess, but probably `aes(x=as.factor(SU), ...)` would solve the problem. – teunbrand Jun 23 '20 at 12:20
  • 1
    Does this answer your question? [Use of scale\_x\_discrete in R ggplot2](https://stackoverflow.com/questions/28742870/use-of-scale-x-discrete-in-r-ggplot2) – xilliam Jun 23 '20 at 12:23
  • The combination of the MarBlo and Waldi comments solved the issue – user13608970 Jun 23 '20 at 15:16

2 Answers2

2

you should make the SU a factor aes(x=factor(SU),fill=factor(SCIENTIFIC_NAME))

MarBlo
  • 4,195
  • 1
  • 13
  • 27
1

You should probably use a discrete scale instead of a continuous scale and make sure the x-axis is a factor:

ggplot(dat, aes(x=factor(SU),fill=factor(SCIENTIFIC_NAME)))+
  geom_bar(width=0.5)+xlab("Sampling Unit (SU)")+
  ylab("Count")+labs(fill="SCIENTIFIC NAME")+
  ggtitle("Inventory per Sampling Unit (SU) by species")+ 
  scale_x_discrete(breaks=c(1,2,3,4,5,6,7,8,10,11,15))
Waldi
  • 39,242
  • 6
  • 30
  • 78