如何用ggplot在facet_grid中设置不同的y轴缩放比例? [英] How to set different y-axis scale in a facet_grid with ggplot?

查看:196
本文介绍了如何用ggplot在facet_grid中设置不同的y轴缩放比例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个数据框 C_Em_df

structure(list(Driver = c("Crop agriculture", "Infrastructure", 
"Mining", "Mixed Agriculture", "Other land use", "Pasture", "Tree crops", 
"Water", "Crop agriculture", "Infrastructure", "Mining", "Mixed Agriculture", 
"Other land use", "Pasture", "Tree crops", "Water", "Crop agriculture", 
"Infrastructure", "Mining", "Mixed Agriculture", "Other land use", 
"Pasture", "Tree crops", "Water", "Crop agriculture", "Infrastructure", 
"Mining", "Mixed Agriculture", "Other land use", "Pasture", "Tree crops", 
"Water"), Period = c("1990-2000", "1990-2000", "1990-2000", "1990-2000", 
"1990-2000", "1990-2000", "1990-2000", "1990-2000", "1990-2000", 
"1990-2000", "1990-2000", "1990-2000", "1990-2000", "1990-2000", 
"1990-2000", "1990-2000", "2000-2005", "2000-2005", "2000-2005", 
"2000-2005", "2000-2005", "2000-2005", "2000-2005", "2000-2005", 
"2000-2005", "2000-2005", "2000-2005", "2000-2005", "2000-2005", 
"2000-2005", "2000-2005", "2000-2005"), Value = c(129536.358373574, 
14089.3660954917, 985.646531415156, 34951.5705930615, 75478.7796771996, 
1001024.77681633, 9673.51414314377, 51631.4446491193, 9.83294102032751, 
1.06950594852475, 0.0748191807457263, 2.65312948831128, 5.7294986378404, 
75.9865238911138, 0.73430421561273, 3.91927761752383, 273356.204972389, 
12040.5899468613, 607.505228212054, 45165.8223684273, 75748.9976185639, 
1221137.74328547, 8851.85933777376, 39629.016246337, 16.3048047540391, 
0.718181861746466, 0.0362357025480948, 2.69399377763239, 4.51818028644936, 
72.8368777437064, 0.527984496372407, 2.36374137750571), n = c("n = 1669", 
"n = 298", "n = 20", "n = 1355", "n = 1623", "n = 10986", "n = 316", 
"n = 466", "n = 1669", "n = 298", "n = 20", "n = 1355", "n = 1623", 
"n = 10986", "n = 316", "n = 466", "n = 783", "n = 151", "n = 7", 
"n = 925", "n = 851", "n = 6039", "n = 211", "n = 244", "n = 783", 
"n = 151", "n = 7", "n = 925", "n = 851", "n = 6039", "n = 211", 
"n = 244"), Type = c("Sum", "Sum", "Sum", "Sum", "Sum", "Sum", 
"Sum", "Sum", "Percentage", "Percentage", "Percentage", "Percentage", 
"Percentage", "Percentage", "Percentage", "Percentage", "Sum", 
"Sum", "Sum", "Sum", "Sum", "Sum", "Sum", "Sum", "Percentage", 
"Percentage", "Percentage", "Percentage", "Percentage", "Percentage", 
"Percentage", "Percentage")), .Names = c("Driver", "Period", 
"Value", "n", "Type"), row.names = c("1", "3", "5", "7", "9", 
"11", "13", "15", "12", "31", "51", "71", "91", "111", "131", 
"151", "2", "4", "6", "8", "10", "122", "14", "16", "21", "41", 
"61", "81", "101", "121", "141", "161"), class = "data.frame")

我想在ggplot中使用参数facet_grid在同一窗口中绘制一个具有绝对值的绘图( Sum )和一个绘图与pe百分比值(百分比)。要做到这一点,我需要在两个图中有两个不同的y轴标度和两个不同的y轴标题。我已经设法做到了下面的代码行,但无法真正实现我想要的。

I want to use the parameter facet_grid in ggplot to plot in the same window one plot with the absolute values (Sum) and one plot with the percentage values (Percentage). To do that I need to have two different y-axis scales and also two different y-axis titles in the two plots. I have managed to do the line of code below, but could not get really what I want.

g <- ggplot(C_Em_df, aes(x = Driver, y = Value, fill = Period, width = .85)) +
  facet_grid(Type~., scales="free")+
geom_bar(position = "dodge", stat = "identity") +
  labs(x = "", y = "Carbon emission (T/Year)") +
  theme(axis.text = element_text(size = 16),
        axis.title = element_text(size = 20), 
        legend.title = element_text(size = 20, face = 'bold'),
        legend.text=  element_text(size=20),
        axis.line = element_line(colour = "black"))+
  scale_fill_grey("Period") +
  theme_classic(base_size = 20, base_family = "") + 
  theme(panel.grid.minor = element_line(colour="grey", size=0.5)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

推荐答案

以下是答案

Here is the answer

#Plot absolute values
p1 <- ggplot(C_Em_df[C_Em_df$Type=="Sum",], aes(x = Driver, y = Value, fill = Period, width = .85)) +
  geom_bar(position = "dodge", stat = "identity") +
  labs(x = "", y = "Carbon emission (T/Year)") +
  theme(axis.text = element_text(size = 16),
        axis.title = element_text(size = 20), 
        legend.title = element_text(size = 20, face = 'bold'),
        legend.text=  element_text(size=20),
        axis.line = element_line(colour = "black"))+
  scale_fill_grey("Period") +
  theme_classic(base_size = 20, base_family = "") + 
  theme(panel.grid.minor = element_line(colour="grey", size=0.5)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

#add the number of observations
foo <- ggplot_build(p1)$data[[1]]
p2<-p1 + annotate("text", x = foo$x, y = foo$y + 50000, label = C_Em_df[C_Em_df$Type=="Sum",]$n, size = 4.5) 

#Plot Percentage values
p3 <- ggplot(C_Em_df[C_Em_df$Type=="Percentage",], aes(x = Driver, y = Value, fill = Period, width = .85)) +
  geom_bar(position = "dodge", stat = "identity") +
  scale_y_continuous(labels = percent_format(), limits=c(0,1))+
  labs(x = "", y = "Carbon Emissions (%)") +
  theme(axis.text = element_text(size = 16),
        axis.title = element_text(size = 20), 
        legend.title = element_text(size = 20, face = 'bold'),
        legend.text=  element_text(size=20),
        axis.line = element_line(colour = "black"))+
  scale_fill_grey("Period") +
  theme_classic(base_size = 20, base_family = "") + 
  theme(panel.grid.minor = element_line(colour="grey", size=0.5)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

# Plot two graphs together
install.packages("gridExtra")
library(gridExtra)
gA <- ggplotGrob(p2)
gB <- ggplotGrob(p3)
maxWidth = grid::unit.pmax(gA$widths[2:5], gB$widths[2:5])
gA$widths[2:5] <- as.list(maxWidth)
gB$widths[2:5] <- as.list(maxWidth)

p4 <- arrangeGrob(
  gA, gB, nrow = 2, heights = c(0.80, 0.80))

这篇关于如何用ggplot在facet_grid中设置不同的y轴缩放比例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆