이 데이터 프레임이 있습니다 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))
여기에 답이 있습니다
#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))
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다