如何使用4个变量的facetting

时间:2014-06-04 13:40:55

标签: r plot ggplot2 bar-chart facet

我的数据如下所示:

structure(list(content = c("Proteins", "Carbohydrates", "Lipids", 
"Ash", "Moisture", "Proteins", "Carbohydrates", "Lipids", "Ash", 
"Moisture", "Proteins", "Carbohydrates", "Lipids", "Ash", "Moisture", 
"Proteins", "Carbohydrates", "Lipids", "Ash", "Moisture", "Proteins", 
"Carbohydrates", "Lipids", "Ash", "Moisture", "Proteins", "Carbohydrates", 
"Lipids", "Ash", "Moisture"), quantity = c(1.58, 4.22, 5.2, 6.5, 
7, 1.95, 5.11, 5.6, 9.7, 7.2, 2.15, 5.05, 4.3, 9, 7.4, 2.71, 
3.47, 5.2, 8, 8, 2.63, 4.77, 7, 7.6, 6.9, 3.01, 5.38, 6.6, 11, 
5.9), city = c("Amravati", "Amravati", "Amravati", "Amravati", 
"Amravati", "Amravati", "Amravati", "Amravati", "Amravati", "Amravati", 
"Amravati", "Amravati", "Amravati", "Amravati", "Amravati", "Amravati", 
"Amravati", "Amravati", "Amravati", "Amravati", "Amravati", "Amravati", 
"Amravati", "Amravati", "Amravati", "Amravati", "Amravati", "Amravati", 
"Amravati", "Amravati"), year = c(2009L, 2009L, 2009L, 2009L, 
2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2011L, 2011L, 
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L), plant = c("Cassia simea", 
"Cassia simea", "Cassia simea", "Cassia simea", "Cassia simea", 
"Cassia simea", "Cassia simea", "Cassia simea", "Cassia simea", 
"Cassia simea", "Cassia simea", "Cassia simea", "Cassia simea", 
"Cassia simea", "Cassia simea", "Cassia simea", "Cassia simea", 
"Cassia simea", "Cassia simea", "Cassia simea", "Cassia simea", 
"Cassia simea", "Cassia simea", "Cassia simea", "Cassia simea", 
"Cassia simea", "Cassia simea", "Cassia simea", "Cassia simea", 
"Cassia simea"), status = c("Non_Polluted", "Non_Polluted", "Non_Polluted", 
"Non_Polluted", "Non_Polluted", "Polluted", "Polluted", "Polluted", 
"Polluted", "Polluted", "Non_Polluted", "Non_Polluted", "Non_Polluted", 
"Non_Polluted", "Non_Polluted", "Polluted", "Polluted", "Polluted", 
"Polluted", "Polluted", "Non_Polluted", "Non_Polluted", "Non_Polluted", 
"Non_Polluted", "Non_Polluted", "Polluted", "Polluted", "Polluted", 
"Polluted", "Polluted")), .Names = c("content", "quantity", "city", 
"year", "plant", "status"), class = "data.frame", row.names = c(NA, 
-30L))

         content quantity     city year        plant       status
1       Proteins     1.58 Amravati 2009 Cassia simea Non_Polluted
2  Carbohydrates     4.22 Amravati 2009 Cassia simea Non_Polluted
3         Lipids     5.20 Amravati 2009 Cassia simea Non_Polluted
4            Ash     6.50 Amravati 2009 Cassia simea Non_Polluted
5       Moisture     7.00 Amravati 2009 Cassia simea Non_Polluted
6       Proteins     1.95 Amravati 2009 Cassia simea     Polluted
7  Carbohydrates     5.11 Amravati 2009 Cassia simea     Polluted
8         Lipids     5.60 Amravati 2009 Cassia simea     Polluted
9            Ash     9.70 Amravati 2009 Cassia simea     Polluted
10      Moisture     7.20 Amravati 2009 Cassia simea     Polluted
11      Proteins     2.15 Amravati 2010 Cassia simea Non_Polluted
12 Carbohydrates     5.05 Amravati 2010 Cassia simea Non_Polluted
13        Lipids     4.30 Amravati 2010 Cassia simea Non_Polluted
14           Ash     9.00 Amravati 2010 Cassia simea Non_Polluted
15      Moisture     7.40 Amravati 2010 Cassia simea Non_Polluted
16      Proteins     2.71 Amravati 2010 Cassia simea     Polluted
17 Carbohydrates     3.47 Amravati 2010 Cassia simea     Polluted
18        Lipids     5.20 Amravati 2010 Cassia simea     Polluted
19           Ash     8.00 Amravati 2010 Cassia simea     Polluted
20      Moisture     8.00 Amravati 2010 Cassia simea     Polluted
21      Proteins     2.63 Amravati 2011 Cassia simea Non_Polluted
22 Carbohydrates     4.77 Amravati 2011 Cassia simea Non_Polluted
23        Lipids     7.00 Amravati 2011 Cassia simea Non_Polluted
24           Ash     7.60 Amravati 2011 Cassia simea Non_Polluted
25      Moisture     6.90 Amravati 2011 Cassia simea Non_Polluted
26      Proteins     3.01 Amravati 2011 Cassia simea     Polluted
27 Carbohydrates     5.38 Amravati 2011 Cassia simea     Polluted
28        Lipids     6.60 Amravati 2011 Cassia simea     Polluted
29           Ash    11.00 Amravati 2011 Cassia simea     Polluted
30      Moisture     5.90 Amravati 2011 Cassia simea     Polluted

我想从数据中制作content vs quantity的条形图或散点图。我的数据框包括两个城市(Amravati& Aurangabad),三年(2009年,2010年,2011年),三种植物(决明子,天竺葵和曼陀罗)以及两种状态(污染和无污染)的数据。

我曾尝试使用ggplot2创建一个条形图,但我只能使用一个facet_wrap进行绘图。我使用的R代码是:

c <- ggplot(amr_nine, aes(content,quantity,fill=plant)) + 
    geom_bar() + 
    facet_wrap(~status) + 
    theme(axis.text.x = element_text(colour = "black"))+
    theme(axis.text.y = element_text(colour = "black"))+ 
    theme(panel.background = element_rect(fill='white', colour='black')) + 
    theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
    scale_fill_manual(values=c("red", "blue", "green"))

我的问题是我们可以有多个方面吗?这样我就可以包括我所有的变量(即城市,年份,植物和地位等)。如果没有任何其他解决方案在单个图中绘制所有这些变量,则非常感谢。

提前致谢...

2 个答案:

答案 0 :(得分:3)

如果您想将所有内容放在一个图中,最好使用facet_grid代替facet_wrap。对于以下三个例子,我还为其他两种植物物种添加了一些数据(对于另外两种植物物种,我使用相同的数量值,但稍微改变了一下)和另一个城市:

ggplot(amr_nine, aes(content,quantity,fill=plant)) + 
  geom_bar(stat="identity") + 
  theme(panel.background = element_rect(fill='white', colour='black'),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        axis.text.x=element_text(angle=90)) +
  scale_fill_manual(values=c("red", "blue", "green")) +
  facet_grid(city ~ status+year)
你会得到类似的东西: enter image description here


但是,考虑到您的数据,我建议为每个城市制作一个多面的情节:

ggplot(amr_nine[amr_nine$city=="Amravati",], aes(content,quantity,fill=plant)) + 
  geom_bar(stat="identity") + 
  theme(panel.background = element_rect(fill='white', colour='black'),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  scale_fill_manual(values=c("red", "blue", "green")) +
  facet_wrap(~status+year)

给出: enter image description here

您可以为其他城市(奥兰加巴德)重复此操作。

除了将条形图堆叠在一起之外,您还可以将position="dodge"添加到代码的geom_bar部分,将它们放在一起。


另一个解决方案是使用折线图而不是条形图:

ggplot(amr_nine, aes(as.factor(year), quantity, color=plant, group=plant)) + 
  geom_line() + 
  theme(panel.background = element_rect(fill='white', colour='black'),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        axis.text.x=element_text(angle=90)) +
  scale_color_manual(values=c("red", "blue", "green")) +
  facet_grid(content ~ city + status)

给出: enter image description here

答案 1 :(得分:1)

你可以有多个方面,而不是所有的条件变量都在同一个公式中

facet_wrap(~status+plant+year+city)

same factedted image