r - geom_line 不显示线
问题描述
我正在尝试绘制一个条形图和折线图的双面图。
但是我无法在没有任何错误代码的情况下在我的折线图中得到这条线。
之前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)
}
})
提前感谢您的帮助!!
解决方案
查看问题,我看到@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)
}
})
推荐阅读
- ruby-on-rails - 如何测试“Model.valid”?在使用 Rspec 的 FactoryBot 生成模型上?
- jacob - 如何传递 BarTender.Messages 类型的参数
- dynamics-crm - 获取子网格中的所有业务推荐字段
- python - 系列:动态换位优化
- c# - 从类函数中获取回调
- python - 如何迭代每个连接的组件标签像素
- apache-nifi - 如何使用 Apache Nifi 删除重复文件?
- python - 修复了“ValueError: no enough values to unpack (expected 3, got 2)”但仍然出现错误
- mysql - 每周更新 MYSQL 字段的最佳方法?
- java - Java中的WeakHashMap如何泄漏内存?