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))
Here is the answer