简体   繁体   English

绘制堆积条形图 - 修正

[英]Plot a stacked barplot - amended

I have 4 dataframes, which all have a column called Results showing Wins, Draws, Losses.我有 4 个数据框,它们都有一个名为 Results 的列,显示了获胜、平局、亏损。 I would like to create a layered histogram as the picture below.我想创建一个分层直方图,如下图所示。 Any idea if it is achievable in R?知道它是否可以在 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.问题是我不知道我应该将 y 轴设置为什么。 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尽管 y 轴停在 800,尽管我在其中一个国家/地区最多有 908 个观测值

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.您基本上只需要四个不同的geom_bar语句。

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.为了演示,我将从ggplot2包附带的mpg数据集创建两个不同的数据帧,因为您没有提供任何数据。

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)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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