How to set different y-axis scale in a facet_grid

2019-04-15 07:29发布

I have this dataframe 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")

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

enter image description here

标签: r ggplot2 facet
1条回答
男人必须洒脱
2楼-- · 2019-04-15 08:06

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))
查看更多
登录 后发表回答