1

I am using ggplot to produce a bar chart stacked by a factor. And I use the following code

ggplot(X) +
  geom_bar(aes(X$Decade, fill=factor(X$Motivation)),
       position='fill') +
  theme(legend.position = "bottom")+
  theme(axis.title.x = element_text(size = 20),
    axis.title.y = element_text(size = 20))+
  theme(text = element_text(size=20))+
  theme(legend.text = element_text(size = 16),legend.title = element_blank())+
  xlab("Decade")+
  ylab("Percentage")+
  scale_y_continuous(labels=percent)

However the program gives the following warning,

Warning message: position_fill requires non-overlapping x intervals

The resulted figure is the below plot

It seems that the warning liers in the decade 1940. When the observations of 1940 are this this

I don't know what's the problem here, since other observations look like those of 1940 and when I removed 1940 observations, no warnings any more.

neilfws
  • 32,751
  • 5
  • 50
  • 63
Terry
  • 179
  • 3
  • 9
  • 1
    Perhaps not related to your problem, but you don't need to use the `x$` in the `aes`. It will look in the data for the variables. – Richard Telford Aug 10 '16 at 20:45
  • 1
    I tried to copy/paste your data and see if I could troubleshoot, but you just posted an image. See if you can [make a reproducible example](http://stackoverflow.com/q/5963269/903061), sharing data with `dput()` or via simulation. Note that we don't need all your data, just enough to demonstrate the problem! – Gregor Thomas Aug 10 '16 at 21:06

1 Answers1

1

Try turning your x variable (Decade) into a factor. Unfortunately we can't test your code because you didn't provide a reprex with the data. Also, as commenters mentioned, you shouldn't be using X$Decade because within ggplot, you've already loaded X as the dataset.

ggplot(X) +
  geom_bar(aes(factor(Decade), fill=factor(Motivation)), position='fill')
Arthur Yip
  • 5,810
  • 2
  • 31
  • 50