-2

I have 4 dataframes, which all have a column called Results showing Wins, Draws, Losses. I would like to create a layered histogram as the picture below. Any idea if it is achievable in R?

enter image description here

This is what I was playing with:

ggplot(results, aes(x = Country, y = ??)) + 


 geom_bar(aes(fill = Performance), stat = "identity")

Problem with this is I don't know what should I set the y axis to be. These are supposed to be counts

Another option I tried which is almost what I want is this:

counts <- table(results$Performance, results$Country)
barplot(counts, main="Game Count per Football Team",
        xlab="Football Teams", ylab = "Game Count", col=c("darkblue","red", "Yellow"),
        legend = rownames(counts))

Although the y axis stop at 800 although I have 908 observations max in one of the countries

  • You need the function `barplot`. There are examples in [QuickR](https://www.statmethods.net/graphs/bar.html) – G5W May 04 '18 at 12:52
  • how do you pass the 4 dataframes to create a Stacked Bar Plot? These dataframes all have a column called Results which can have values Wins, Draws, Losses. Sorry but I am completely new to R – user9740061 May 04 '18 at 13:00
  • 1
    Please read through [this](https://stackoverflow.com/help/how-to-ask) and [this](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Even if you are new to R and your attempt doesn't actually work, people are more likely to help if you show what you've tried. – Z.Lin May 04 '18 at 13:55
  • It would be a lot easier if you had all of this in just one dataframe rather than four dataframes. Do you absolutely have to have this broken out into four different dataframes? – LetEpsilonBeLessThanZero May 04 '18 at 17:43

1 Answers1

0

Well, I can give you some code that will show you how you could do this. You basically would just want four different geom_bar statements.

To demonstrate, I'll create two different dataframes from the mpg dataset that comes with the ggplot2 package, because you didn't provide any data.

library(tidyverse)

# I'm making two different data frames from the
# 'mpg' dataset, which comes with the ggplot package
mpg$year = as.character(mpg$year)
df1 = filter(mpg, year == "1999")
df2 = filter(mpg, year == "2008")

plot = ggplot() +
  geom_bar(data=df1
           , aes(x = year, y = hwy, fill = manufacturer)
           , stat = "identity") +
  geom_bar(data=df2
           , aes(x = year, y = hwy, fill = manufacturer)
           , stat = "identity")

print(plot)

enter image description here

LetEpsilonBeLessThanZero
  • 2,395
  • 2
  • 12
  • 22
  • You don't need 2 data frames and 2 `geom_bar` calls, just work with long data. Use `mpg %>% filter(year %in% c(1999, 2008))` to have a long df that has those two years of data – camille May 05 '18 at 21:46
  • 1
    I completely agree with you, but OP said they have four different data frames so I tried to accommodate for that. I left a comment on OP's post suggesting that they try to work with one dataframe rather than having four. – LetEpsilonBeLessThanZero May 06 '18 at 13:34