0

I'm trying to create a geom bar from a subset previously created to compare different behaviours in terrories with and without wolves present. It worked fine on the complete dataset (wolf):

dataall <-data.frame(wolf$Behaviour, territory="SAND", territory = "FALSK")

longall<-melt(wolf$Behaviour, id=c("Behaviour"))
ggplot(longall)+
  geom_bar(aes(x=wolf$Behaviour, fill = territory),position = "dodge" )+
  scale_fill_manual("Wolf Territory", values = c("aquamarine3","darkorange"),labels=c("Active", "Inactive"))+
  labs(x="Behaviour",y="Percentage") +
  theme_bw(base_size = 14)+
  scale_y_continuous(labels = scales::percent)+
  aes(y=stat(count)/sum(stat(count)))

but when entering with the subset it gives the error code:

badger<-subset(wolf,Species=="badger")
databadger <-data.frame(badger$Behaviour, territory="SAND", territory = "FALSK")
longbadger<-melt(badger$Behaviour, id=c("Behaviour"))

ggplot(longbadger)+
  geom_bar(aes(x=badger$Behaviour, fill = territory),position = "dodge" )+
                 scale_fill_manual("Wolf Territory", values = c("aquamarine3","darkorange"),labels=c("Active", "Inactive"))+
                 labs(x="Behaviour",y="Percentage") +
  theme_bw(base_size = 14)+
  scale_y_continuous(labels = scales::percent)+
  aes(y=stat(count)/sum(stat(count))) 

I've tried with just the original subset and with a melted long version and neither seem to work. Can anybody figure out what I'm missing? Data can be found here: https://drive.google.com/file/d/1JvRspQzLfy6BY7d-4jgggfzmW818mVsw/view?usp=sharing

1 Answers1

0

Your x aesthetic uses badger but your fill comes from longbadger. I assume that those two data frames have different nrows.

Try having Behaviour and territory in a single data frame. It is generally a bad practice to use multiple data frames in one ggplot.

NiklasvMoers
  • 309
  • 2
  • 13
  • They do. I've tried having just longbadger in both x aes and in fill but that gives the same error message. Behaviour and territory are both columns of the same dataset. For some reason it work with the entire dataset, without the species subset. So I don't understand why Behaviour and territory doesn't work when they're both still part of the badger subset..? – MeAarsleff Jul 01 '20 at 10:03
  • I see. I have downloaded the data and there are a couple of things that stood out to me. ```databadger``` has two parameters called ```territory```. What exactly are you trying to to there? Secondly, why are you adding ```aes()``` at the end of the ggplot? Why don't you use the y aesthetic in the first ```aes()``` call? – NiklasvMoers Jul 01 '20 at 10:21
  • Yes, so the true/false parameters are whether or not the area is classified as active (true) or inactive (false) wolf territories. I'm trying to compare behaviours of different species in those two territories. Does that answer the question? whenever I added a y aesthetic in the first call I got an error message telling me, there could only be an x or a y in that call. But for some reason I runs when moved to the end – MeAarsleff Jul 01 '20 at 10:33
  • yes! I didn't know how to do it in a comment, so I've added it at the top in the question – MeAarsleff Jul 02 '20 at 10:04
  • The above code does not work for me. Why does ```dataall``` have two ```territory```s? Did you intend to use ```FALSE``` instead of ```FALSK```? And are you trying to subset, i.e. only have the rows where ```territory``` is ```FALSE```? Or do you want to add a column which's entries are all ```FALSE```? ```longall``` is a data frame with only one column when I run the code. What does it look like for you? If you tell me exactly what you are trying to accomplish, I might be able to help you. However, at the moment, I cannot help you, I fear. – NiklasvMoers Jul 02 '20 at 16:08