使用ggplot绘制几个直方图

时间:2016-01-04 12:40:53

标签: r ggplot2

我正在寻找一种相互映射数据集子集的方法。我有以下df,那种看起来像这样(geboortejaar =" birthyear" geboorteland ="出生国和#34;):

System.IO.File.Copy(
     Server.MapPath("~\FolderInsideProjectFolder\", "fileName.extension"), 
     @"\\SERVERIP\aFolder\bFolder" + "fileName.extension" 
     [, true ] // Optional if you want the file to be over-written or not
);

库(GGPLOT2)

     Geboortejaar     Geboorteland       month
1    23-9-1980      Germany        9
2   18-12-1968      Germany        12
3   29-12-1967      Netherlands    12

但这给了我一个叠加的直方图。我正在寻找两个直方图。

有关如何实现这一目标的任何想法?

2 个答案:

答案 0 :(得分:4)

按照图形语法,即将颜色映射到数据并指定比例。然后,您需要指定使用position_indentity而不是默认position_stack

ggplot(df, aes(x = month)) +
  geom_histogram(aes(fill = Geboorteland), position = "identity", alpha = 0.2) +
  scale_fill_manual(values = c("Germany" = "blue", "Netherlands" = "red"))

答案 1 :(得分:1)

我通常更喜欢这个用于比较两个发行版:

ggplot(df, aes(x = month, y = ..density.., fill = Geboorteland)) +
  geom_histogram(position = 'dodge')

你应该调整' binwidth'在geom_histogram()中,以满足您的需求。