简体   繁体   中英

Plot a stacked barplot - amended

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?

在此处输入图片说明

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

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)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM