简体   繁体   English

geom_line 不显示线

[英]geom_line don't show line

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.之前google了一下,有人说画线图的时候可以加aes(group=1)。 But with the aes(group=1), it can't separate the data into groups as I ordered.但是使用 aes(group=1),它无法按照我的命令将数据分成组。

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!!在此先感谢您的帮助!!

Reviewing the question and I see @MrFlick provided the answer, but as a comment.查看问题,我看到@MrFlick 提供了答案,但作为评论。 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)
    }
      
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM