我正在嘗試繪制一個條形圖和折線圖的雙重面圖。
但是我無法在沒有任何錯誤代碼的情況下獲得折線圖中的線。
之前google了一下,有人說畫線圖的時候可以加aes(group=1)。但是使用 aes(group=1),它無法按照我的命令將資料分成組。
我在其他資料集上使用了此代碼,它們運行良好。我不知道他們為什么不處理這個資料集。
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)
}
})
在此先感謝您的幫助!!
uj5u.com熱心網友回復:
查看問題,我看到@MrFlick 提供了答案,但作為評論。在答案下方添加,以便可以關閉問題:
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)
}
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/341819.html
