我有一个长的数据框格式,它是这样创建的:
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')
如果我们想在我们可以利用的情节值label
与geom_text
ggplot(data = test_df,
aes(Rondenummer, puntenvector, fill = team, label = puntenvector)) +
geom_bar(stat='identity') +
geom_text(position = position_stack(vjust = 0.5))
最后,如果我们要扭转的订单Rondenummer
后coord_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] 删除。
我来说两句