如何使用ggplot在facet_grid中设置不同的y轴刻度?

时间:2015-01-23 13:52:17

标签: r ggplot2 facet

我有这个数据框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)的图和一个带有百分比值(Percentage)的图。要做到这一点,我需要在两个图中有两个不同的y轴刻度和两个不同的y轴标题。我已经成功完成了下面的代码行,但无法真正得到我想要的。

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))

enter image description here

1 个答案:

答案 0 :(得分:2)

以下是答案

#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))