简体   繁体   English

百分比 plot 使用 ggplot2

[英]percentage plot using ggplot2

I would like to plot something like the image below but I have an error of "stat_count() can only have an x or y aesthetic.".我想 plot 类似于下图,但我有一个错误“stat_count() 只能有 x 或 y 美学。”。 So I change it to stat="identity".所以我把它改成stat="identity"。 But the outcome is not the same as the example I found online.但是结果和我在网上找到的例子不一样。

Example found online:网上找的例子:

example is found from: https://sebastiansauer.github.io/percentage_plot_ggplot2_V2/示例来自: https://sebastiansauer.github.io/percentage_plot_ggplot2_V2/ 网上找的例子

My code我的代码

  geom_bar(aes(y=pct_change, fill=factor(Year)), stat="count")+
  geom_text(aes( label = scales::percent(pct_change),
                 y= pct_change ), stat= "count", vjust = -.5) +
  ylab("Avg Unit Price")  +
  facet_grid(~Type.of.Sale) +
  scale_y_continuous(labels = scales::percent)

Here is an example with the mtcars dataset how you could achieve your task:这是mtcars数据集的示例,您可以如何完成任务:

library(tidyverse)
ggplot(mtcars, aes(x = cyl, group=am))+
  geom_bar(aes(y=..prop.., fill=factor(..x..)), stat="count")+
  geom_text(aes( label = scales::percent(..prop..),
                 y= ..prop..), stat= "count", vjust = -.5) +
  ylab("Avg Unit Price")  +
  facet_grid(~am) +
  scale_y_continuous(labels = scales::percent)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM