I'm trying to plot the dual facet plot of one bar graph and line graph.
But I can't get the line in my line graph without any error code.
I googled it before, and some people said they could add the aes(group=1) when drawing the line graph. But with the aes(group=1), it can't separate the data into groups as I ordered.
I used this code on other datasets, and they work well. I don't know why they don't work on this data set.
library(lubridate)
library(dplyr)
library(ggplot2)
zone <- structure(list(location = c("rm", "jj", "jsy", "hyh"), cropland_2018 =
c(0.92330383480826,
0.887755102040816, 0.813559322033898, 0.771653543307087), urban_2018 = c(0.0176991150442478,
0.0283446712018141, 0, 0.00393700787401575), nature_2018 = c(0.0589970501474926,
0.0839002267573696, 0.186440677966102, 0.224409448818898), cropland_change_rate2018_2001 =
c(0, 0, 0, -0.069620253164557), urban_change_rate2018_2001 = c(0.0526315789473684,
-0.281553398058252, 0, 0), nature_change_rate2018_2001 = c(-0.00294117647058824,
-0.00898876404494382, -0.0833333333333333, 0.0363636363636364
)), row.names = c(NA, -4L), class = c("tbl_df", "tbl", "data.frame"
))
z=zone %>% pivot_longer(cols=cropland_2018:nature_2018, names_to = "landcover", values_to =
"area_percent")
df=z %>% pivot_longer(cols=cropland_change_rate2018_2001:nature_change_rate2018_2001, names_to = "change", values_to = "rate_20y")
dfF <- rbind(
data.frame(
location=df$location,
num=df$rate_20y,
group=df$change,
what="rate_20y"),
data.frame(location=df$location,
num=df$area_percent, group=df$landcover,what="area_percent")
)
secondFacet <- FALSE # see below
ggplot(data = dfF, mapping = aes(x = location, y = num,fill=group,color=group))
facet_grid(what~., scale = "free")
geom_line(data=dfF[dfF$what=="rate_20y",],size = 2)
scale_color_manual(values=c("#999999", "#999999", "#E69F00", "#E69F00",
"#56B4E9","#56B4E9"))
geom_bar(data=dfF[dfF$what=="area_percent",],position="dodge", stat="identity")
scale_fill_manual(values=c("#999999", "#999999", "#E69F00", "#E69F00", "#56B4E9","#56B4E9"))
theme_bw()
scale_y_continuous(name = NULL, labels = function(b) {
if(!secondFacet) {
secondFacet <<- TRUE # this is a little cray (and relies on dtF seq = facet seq; works though)
return(paste0(round(b * 100, 0), "%"))
}else{
return(b)
}
})
Thanks in advance for any help!!
CodePudding user response:
Reviewing the question and I see @MrFlick provided the answer, but as a comment. Adding below the answer, so that the question can be closed:
library(lubridate)
library(tidyverse)
library(ggplot2)
zone <- structure(list(location = c("rm", "jj", "jsy", "hyh"), cropland_2018 =
c(0.92330383480826,
0.887755102040816, 0.813559322033898, 0.771653543307087), urban_2018 = c(0.0176991150442478,
0.0283446712018141, 0, 0.00393700787401575), nature_2018 = c(0.0589970501474926,
0.0839002267573696, 0.186440677966102, 0.224409448818898), cropland_change_rate2018_2001 =
c(0, 0, 0, -0.069620253164557), urban_change_rate2018_2001 = c(0.0526315789473684,
-0.281553398058252, 0, 0), nature_change_rate2018_2001 = c(-0.00294117647058824,
-0.00898876404494382, -0.0833333333333333, 0.0363636363636364
)), row.names = c(NA, -4L), class = c("tbl_df", "tbl", "data.frame"
))
z=zone %>% pivot_longer(cols=cropland_2018:nature_2018, names_to = "landcover", values_to =
"area_percent")
df=z %>% pivot_longer(cols=cropland_change_rate2018_2001:nature_change_rate2018_2001, names_to = "change", values_to = "rate_20y")
dfF <- rbind(
data.frame(
location=df$location,
num=df$rate_20y,
group=df$change,
what="rate_20y"),
data.frame(location=df$location,
num=df$area_percent, group=df$landcover,what="area_percent")
)
secondFacet <- FALSE # see below
ggplot(data = dfF, mapping = aes(x = location, y = num,fill=group,color=group))
facet_grid(what~., scale = "free")
geom_line(aes(group=group), data=dfF[dfF$what=="rate_20y",],size = 2)
scale_color_manual(values=c("#999999", "#999999", "#E69F00", "#E69F00",
"#56B4E9","#56B4E9"))
geom_bar(data=dfF[dfF$what=="area_percent",],position="dodge", stat="identity")
scale_fill_manual(values=c("#999999", "#999999", "#E69F00", "#E69F00", "#56B4E9","#56B4E9"))
theme_bw()
scale_y_continuous(name = NULL, labels = function(b) {
if(!secondFacet) {
secondFacet <<- TRUE # this is a little cray (and relies on dtF seq = facet seq; works though)
return(paste0(round(b * 100, 0), "%"))
}else{
return(b)
}
})