简体   繁体   中英

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.". So I change it to 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/ 网上找的例子

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:

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)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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