我正在绘制一个分类变量,而不是显示每个类别值的计数.
我正在寻找一种方法来ggplot显示该类别中的值的百分比.当然,有可能用计算的百分比创建另一个变量并绘制一个变量,但我必须做几十次,我希望在一个命令中实现它.
我正在尝试类似的东西
qplot(mydataf) +
stat_bin(aes(n = nrow(mydataf), y = ..count../n)) +
scale_y_continuous(formatter = "percent")
Run Code Online (Sandbox Code Playgroud)
但我必须错误地使用它,因为我有错误.
为了轻松重现设置,这里有一个简化的例子:
mydata <- c ("aa", "bb", NULL, "bb", "cc", "aa", "aa", "aa", "ee", NULL, "cc");
mydataf <- factor(mydata);
qplot (mydataf); #this shows the count, I'm looking to see % displayed.
Run Code Online (Sandbox Code Playgroud)
在实际情况中,我可能会使用ggplot而不是qplot,但使用stat_bin的正确方法仍然无法使用.
我也试过这四种方法:
ggplot(mydataf, aes(y = (..count..)/sum(..count..))) +
scale_y_continuous(formatter = 'percent');
ggplot(mydataf, aes(y = (..count..)/sum(..count..))) +
scale_y_continuous(formatter = 'percent') + geom_bar();
ggplot(mydataf, aes(x = levels(mydataf), y = (..count..)/sum(..count..))) +
scale_y_continuous(formatter = 'percent');
ggplot(mydataf, aes(x = levels(mydataf), y = (..count..)/sum(..count..))) +
scale_y_continuous(formatter = 'percent') + geom_bar();
Run Code Online (Sandbox Code Playgroud)
但所有4给出:
Run Code Online (Sandbox Code Playgroud)Error: ggplot2 doesn't know how to deal with data of class factor
对于简单的情况,会出现相同的错误
ggplot (data=mydataf, aes(levels(mydataf))) +
geom_bar()
Run Code Online (Sandbox Code Playgroud)
所以它显然是关于如何ggplot与单个载体相互作用的.我正在挠头,谷歌搜索该错误给出了一个结果.
And*_*rew 214
由于回答了这一问题,因此对ggplot语法进行了一些有意义的更改.总结上述评论中的讨论:
require(ggplot2)
require(scales)
p <- ggplot(mydataf, aes(x = foo)) +
geom_bar(aes(y = (..count..)/sum(..count..))) +
## version 3.0.0
scale_y_continuous(labels=percent)
Run Code Online (Sandbox Code Playgroud)
这是一个可重现的例子,使用mtcars:
ggplot(mtcars, aes(x = factor(hp))) +
geom_bar(aes(y = (..count..)/sum(..count..))) +
scale_y_continuous(labels = percent) ## version 3.0.0
Run Code Online (Sandbox Code Playgroud)
这个问题目前是google上'ggplot count vs百分比直方图'的第一个问题,所以希望这有助于提取当前所有关于已接受答案的评论中的信息.
备注:如果hp未设置为因子,ggplot将返回:
Ram*_*ath 58
这个修改过的代码应该可行
p = ggplot(mydataf, aes(x = foo)) +
geom_bar(aes(y = (..count..)/sum(..count..))) +
scale_y_continuous(formatter = 'percent')
Run Code Online (Sandbox Code Playgroud)
如果您的数据有NA并且您不希望它们包含在图中,请将na.omit(mydataf)作为参数传递给ggplot.
希望这可以帮助.
Fab*_*wig 48
使用ggplot2 2.1.0版本
+ scale_y_continuous(labels = scales::percent)
Run Code Online (Sandbox Code Playgroud)
Oli*_* Ma 36
截至2017年3月,凭借ggplot22.2.1,我认为最佳解决方案在Hadley Wickham的R for data science book中有所解释:
ggplot(mydataf) + stat_count(mapping = aes(x=foo, y=..prop.., group=1))
Run Code Online (Sandbox Code Playgroud)
stat_count计算两个变量:count默认使用,但您可以选择使用prop哪个显示比例.
Sam*_*rke 19
如果您想要y轴上的百分比并在条形图上标记:
library(ggplot2)
library(scales)
ggplot(mtcars, aes(x = as.factor(am))) +
geom_bar(aes(y = (..count..)/sum(..count..))) +
geom_text(aes(y = ((..count..)/sum(..count..)), label = scales::percent((..count..)/sum(..count..))), stat = "count", vjust = -0.25) +
scale_y_continuous(labels = percent) +
labs(title = "Manual vs. Automatic Frequency", y = "Percent", x = "Automatic Transmission")
Run Code Online (Sandbox Code Playgroud)
添加条形标签时,您可能希望省略y轴以获得更清晰的图表,方法是添加到结尾:
theme(
axis.text.y=element_blank(), axis.ticks=element_blank(),
axis.title.y=element_blank()
)
Run Code Online (Sandbox Code Playgroud)
请注意,如果您的变量是连续的,则必须使用 geom_histogram(),因为该函数将按“bins”对变量进行分组。
df <- data.frame(V1 = rnorm(100))
ggplot(df, aes(x = V1)) +
geom_histogram(aes(y = 100*(..count..)/sum(..count..)))
# if you use geom_bar(), with factor(V1), each value of V1 will be treated as a
# different category. In this case this does not make sense, as the variable is
# really continuous. With the hp variable of the mtcars (see previous answer), it
# worked well since hp was not really continuous (check unique(mtcars$hp)), and one
# can want to see each value of this variable, and not to group it in bins.
ggplot(df, aes(x = factor(V1))) +
geom_bar(aes(y = (..count..)/sum(..count..)))
Run Code Online (Sandbox Code Playgroud)
如果您想要百分比标签但y轴上的实际Ns,请尝试以下方法:
library(scales)
perbar=function(xx){
q=ggplot(data=data.frame(xx),aes(x=xx))+
geom_bar(aes(y = (..count..)),fill="orange")
q=q+ geom_text(aes(y = (..count..),label = scales::percent((..count..)/sum(..count..))), stat="bin",colour="darkgreen")
q
}
perbar(mtcars$disp)
Run Code Online (Sandbox Code Playgroud)
这是分面数据的解决方法.(@Andrew接受的答案在这种情况下不起作用.)想法是使用dplyr计算百分比值,然后使用geom_col创建绘图.
library(ggplot2)
library(scales)
library(magrittr)
library(dplyr)
binwidth <- 30
mtcars.stats <- mtcars %>%
group_by(cyl) %>%
mutate(bin = cut(hp, breaks=seq(0,400, binwidth),
labels= seq(0+binwidth,400, binwidth)-(binwidth/2)),
n = n()) %>%
group_by(cyl, bin) %>%
summarise(p = n()/n[1]) %>%
ungroup() %>%
mutate(bin = as.numeric(as.character(bin)))
ggplot(mtcars.stats, aes(x = bin, y= p)) +
geom_col() +
scale_y_continuous(labels = percent) +
facet_grid(cyl~.)
Run Code Online (Sandbox Code Playgroud)
这是情节:
从ggplot2 3.3 版本开始,我们可以使用方便的after_stat()功能。
我们可以做一些类似于@Andrew 的回答,但不使用..语法:
# original example data
mydata <- c("aa", "bb", NULL, "bb", "cc", "aa", "aa", "aa", "ee", NULL, "cc")
# display percentages
library(ggplot2)
ggplot(mapping = aes(x = mydata,
y = after_stat(count/sum(count)))) +
geom_bar() +
scale_y_continuous(labels = scales::percent)
Run Code Online (Sandbox Code Playgroud)

您可以在geom_和stat_函数的文档中找到所有可用的“计算变量” 。例如,对于geom_bar(),您可以访问count和prop变量。(请参阅计算变量的文档。)
关于您的NULL值的一个评论:当您创建向量时,它们会被忽略(即您最终得到一个长度为 9,而不是 11 的向量)。如果您真的想跟踪丢失的数据,则必须NA改用(ggplot2 会将 NA 放在图的右端):
# use NA instead of NULL
mydata <- c("aa", "bb", NA, "bb", "cc", "aa", "aa", "aa", "ee", NA, "cc")
length(mydata)
#> [1] 11
# display percentages
library(ggplot2)
ggplot(mapping = aes(x = mydata,
y = after_stat(count/sum(count)))) +
geom_bar() +
scale_y_continuous(labels = scales::percent)
Run Code Online (Sandbox Code Playgroud)

由reprex 包(v1.0.0)于2021年 2 月 9 日创建
(请注意,使用chr或fct数据不会对您的示例产生影响。)
| 归档时间: |
|
| 查看次数: |
201658 次 |
| 最近记录: |