ggplot geom_bar的比例 [英] Proportion with ggplot geom_bar

查看:90
本文介绍了ggplot geom_bar的比例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与ggplot相同的最简单方法是什么?

What is the simplest way to do with ggplot the same as done here:

我需要打电话给prop.table还是有一个简单的方法?

Do I need call prop.table or maybe there is a simplier way?

可复制的示例:

x <- c("good", "good", "bad", "bad", "bad", "bad", "perfect", "perfect", "perfect")
y <- c("exercise1", "exercise2", "exercise3", "exercise1", "exercise2", "exercise3", "exercise1", "exercise2", "exercise3")
dt <- data.frame(x, y)

ggplot(dt, aes(x, fill = y)) + geom_bar()

推荐答案

这是与上一个类似的问题参数将条形图缩放到100%的高度. scale_y_continuous(labels = scales::percent)命令将频率标度从0-1更改为0-100%.

This is a similar question to this previous one here. You can use the position = "fill" argument within ggplot to scale bars to 100% height. The scale_y_continuous(labels = scales::percent) command changes the frequency scale from 0-1 to 0-100%.

library(ggplot2)

x <- c("good", "good", "bad", "bad", "bad", "bad", "perfect", "perfect", "perfect") 
y <- c("exercise1", "exercise2", "exercise3", "exercise1", "exercise2", "exercise3", "exercise1", "exercise2", "exercise3") 
dt <- data.frame(x, y)

# Build plot
ggplot(dt, aes(x, fill = y)) +
  geom_bar(position = "fill") +
  scale_y_continuous(labels = scales::percent)

这篇关于ggplot geom_bar的比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆