如何使用长数据框格式在ggplot中制作堆叠式条形图?

纳布

我有一个长的数据框格式,它是这样创建的:

testAppVectorJG <- c(17, 155, 200, 200, 382, 499, 548, 548, 642, 642, 699, 699)
testVectorJG <- c(testAppVectorJG[1], diff(testAppVectorJG))

testAppVectorJW <- c(145, 209, 366, 548, 548, 613, 746, 928, 1064, 1266, 1371, 1573)
testVectorJW <- c(testAppVectorJW[1], diff(testAppVectorJW))

test_df <- data.frame(puntenvector = c(testVectorJG, testVectorJW),
                      team = c(rep("Jasper & Gijs", length(testAppVectorJG)),
                               rep("Jaap & Wil", length(testAppVectorJW))),
                      Rondenummer = as.factor(1:length(testVectorJG)))

我想制作一个堆叠的条形图,其中每个“ Rondenummer”都带有一个条形图(即所打的回合数)。我想查看每支球队每轮得分的百分比/分布。

到目前为止,我已经尝试过:

ggplot(data = test_df, aes(Rondenummer)) +
    geom_bar(aes(x = puntenvector, fill = team))

但是然后我得到:

Warning message:
position_stack requires non-overlapping x intervals

而且根本不是我想要的情节。我如何实现这个相当简单的情节?

罗纳克·沙

也许是这样的吗?

library(ggplot2)

ggplot(data = test_df, aes(Rondenummer, puntenvector, fill = team)) +
   geom_bar(stat='identity')

在此处输入图片说明

如果我们想在我们可以利用的情节值labelgeom_text

ggplot(data = test_df, 
      aes(Rondenummer, puntenvector, fill = team, label = puntenvector)) +
      geom_bar(stat='identity') +
      geom_text(position = position_stack(vjust = 0.5))

在此处输入图片说明

最后,如果我们要扭转的订单Rondenummercoord_flip(),我们可以添加scale_x_discrete和反向水平。

 ggplot(data = test_df, 
   aes(Rondenummer, puntenvector, fill = team, label = puntenvector)) +
   geom_bar(stat='identity') +
   geom_text(position = position_stack(vjust = 0.5)) +
   coord_flip() + 
   scale_x_discrete(limits = rev(levels(test_df$Rondenummer))) 

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用ggplot2从一组宽数据制作堆叠的条形图?

为ggplot中的分类和数值数据制作堆叠比例条形图

如何使ggplot订购堆叠条形图

如何在ggplot中绘制堆叠条形图以获取数据类型列表?

R-如何使用所有值的标签或可以设置格式的文本表制作堆叠的条形图

如何在 ggplot/R 中按特定顺序制作堆叠条形图,同时保持特定的配色方案?

如何使用geom_bar在ggplot2中制作连接的条形图?

情节:如何从单个轨迹制作堆叠的条形图?

如何在ggplot2中绘制(复杂的)堆叠条形图,而无需复杂的手动数据聚合

如何在 R 中使用 ggplot2 创建带有数据标签的非堆叠条形图?

熊猫/ matplotlib中的堆叠式交错水平条形图

ggplot基于数据框中的值对堆叠的条形图进行重新排序

使用行作为数据点的ggplot2堆叠条形图

使用ggplot2的堆叠条形图-数据可视化

使用ggplot2的堆叠条形图的数据帧的保留顺序

ggplot中具有单列的堆叠条形图?

反转堆叠条形图的顺序-在ggplot中突出显示

ggplot中宽度可变的堆叠条形图

堆叠条形图ggplot2中的标签顺序

ggplot2:使用组均值堆叠条形图

使用 ggplot2 的 5 维堆叠条形图

使用 ggplot2 堆叠条形图

如何在R中的ggplot中更改堆叠条形图的位置?

R:如何在ggplot中翻转堆叠的并排条形图

如何使用ggplot2将连续数作为离散数字堆叠条形图

如何为三组数据创建堆叠的条形图?

R中的堆叠条形图:对数据重新排序

在 Python 数据框中使用 group by 堆叠条形图

如何使用ggplot绘制条形图