条形图中的条形图与ggplot2

时间:2017-08-15 01:10:22

标签: r ggplot2

是否可以使用ggplot2在R中的条形图中绘制条形图,如下图所示。我想比较预期值和实际值,我想生成一个类似于下图的数字。我提供了一些虚拟代码:

 library(tidyverse)

df = data_frame(name = letters[1:10],profit = rnorm(mean = 100, sd = 20, n = 10),
            target = profit + rnorm(mean = 10, sd = 10, n = 10))

   df %>% ggplot(aes(x = name, y = profit)) + geom_bar(stat = "identity") 

enter image description here

1 个答案:

答案 0 :(得分:4)

是的,有可能,请查看ggplot文档

library(tidyverse)

df = data_frame(name = letters[1:10],profit = rnorm(mean = 100, sd = 20, n = 10),
                target = profit + rnorm(mean = 10, sd = 10, n = 10))

ggplot(df,aes(x=name,y=profit))+geom_bar(stat= "identity")+
geom_bar(aes(x= name,y=target),stat= "identity", width = 0.1,col = "green",fill="green")