我试图绘制好的stacked percent barchart使用ggplot2.我已经阅读了一些材料,几乎可以设法绘制,我想要什么.另外,我附上材料,它可能在一个地方有用:
如何在不创建摘要数据框的情况下在ggplot2中标记堆积条形图?
R堆积百分比条形图,包含二元因子和标签的百分比(使用ggplot)
我的问题是我不能放在labels我想要的地方- 在酒吧中间.
您可以在上面的图片中看到问题 - 标签看起来很糟糕,并且彼此重叠.
我现在正在寻找的是:
如何在酒吧(区域)的中间放置标签
如何绘制不是所有标签,但例如哪些标签超过10%?
如何解决重叠问题?
对于Q 1.@MikeWise建议可能的解决方案.但是,我仍然无法处理这个问题.
另外,我附上了可重复的例子,我是如何绘制这个grahp的.
library('plyr')
library('ggplot2')
library('scales')
set.seed(1992)
n=68
Category <- sample(c("Black", "Red", "Blue", "Cyna", "Purple"), n, replace = TRUE, prob = NULL)
Brand <- sample("Brand", n, replace = TRUE, prob = NULL)
Brand <- paste0(Brand, sample(1:5, n, replace = TRUE, prob = NULL))
USD <- abs(rnorm(n))*100
df <- data.frame(Category, Brand, USD)
# Calculate the percentages …Run Code Online (Sandbox Code Playgroud) 背景:我是 R 和 ggplot2 的新手,希望得到一个简单的答案,在相同的情况下可以让其他人受益。
问题:在 ggplot2 中使每个条形加起来为 100% 并在每个条形内显示百分比标签的简单方法是什么?
目标: 我想要的样子(每列总计 100%,每列内带有百分比标签)。
当前图表:这是我目前的条形图;基于“mtcars”数据集(它目前的样子)。下面我有“当前图表”:
library(ggplot2) # loads ggplot2
data <- mtcars # gets the mtcars dataset
#INFO: A simple barchart with X-axis being Nr of Cylinders, fill color indicating whether or not the car model has an Automatic gearbox..
# ..and Y-axis showing the count of cars by Nr of Cylinders
ggplot(mtcars, aes(x=cyl, fill = factor(am))) +
geom_bar() +
stat_count(geom …Run Code Online (Sandbox Code Playgroud)