gpt4 book ai didi

r - 与 ggplot geom_bar 的比例

转载 作者:行者123 更新时间:2023-12-02 11:17:04 25 4
gpt4 key购买 nike

使用 ggplot 的最简单方法是什么,与此处相同:

enter image description here

我需要调用 prop.table 还是有更简单的方法?

可重现的示例:

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()

最佳答案

这是一个与上一个类似的问题 here 。您可以在 ggplot 中使用 position = "fill" 参数将条形缩放至 100% 高度。 scale_y_continuous(labels = scales::percent) 命令将频率范围从 0-1 更改为 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)

enter image description here

关于r - 与 ggplot geom_bar 的比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46984296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com