【问题标题】:Combining two line plots from different two data frames组合来自不同两个数据框的两个线图
【发布时间】:2019-12-01 15:21:20
【问题描述】:

我有两个折线图显示:一个显示六个物种在四个采样中的下降,另一个显示相同六个物种的总下降。我想创建一个单独显示六个物种和总下降的图。

以下是我的六个物种的数据框示例

 Week       Species      Unit
   1           A           13
   1           B           24
   2           B           15
   2           C           32
   3           C           43
   4           D           32

下面是我用来创建显示六种衰退的折线图的代码

Species_Change<-ggplot(Total_Count, aes(x=Week, y=Unit, group=Scientific.name, color=Scientific.name)) +
  geom_point()+
  geom_line()+
  scale_colour_manual(values = c("yellow", "orange 2", "purple", "maroon 2", "blue", "purple 4","green"))+
  ylab("Total number of floral units over the four habitats")+
  xlab("Sampling round")+
  labs(col="Plant species")

下面是一个数据框样本,显示了采样期间所有物种的总数

Week           Unit
 1              32
 2              55
 3              73
 4              62

下面显示了用于创建第二个折线图的代码,该图显示了所有物种在采样轮次中的总数

Total_Change<-ggplot(Total_Round, aes(x=Week, y=Unit, )) +
  geom_point()+
  geom_line()+
  ylab("Total number of floral units over the four habitats")+
  xlab("Sampling round")+
  labs(col="Plant species")

【问题讨论】:

    标签: r ggplot2 linegraph


    【解决方案1】:

    我不确定这是否是你想要的,但你可以在你的Total_countdata.frame 中添加一个名为“total”的新物种。使用您提供的数据:

    数据
    dput(Total_Count)
    structure(list(Week = c(1L, 1L, 2L, 2L, 3L, 4L), Species = structure(c(1L, 2L, 2L, 3L, 3L, 4L), .Label = c("A", "B", "C", "D"), class = "factor"), Unit = c(13L, 24L, 15L, 32L, 43L, 32L)), row.names = c(NA, -6L), class = "data.frame")
    dput(Total_Round)
    structure(list(Week = c(1, 2, 3, 4), Unit = c(32, 55, 73, 62)), class = "data.frame", row.names = c(NA, -4L))
    
    代码
    Total_Round$Species <- factor(rep("Total",nrow(Total_Round))) # Create total species
    df <- rbind(Total_Count, Total_Round) # Merge both data.frames
    AllwithTotal_Species_Change<-ggplot(df, aes(x=Week, y=Unit, group=Species, color=Species)) +
      geom_point()+
      geom_line()+
      scale_colour_manual(values = c("yellow", "orange 2", 
                                     "purple", "maroon 2", 
                                     "blue", "purple 4",
                                     "green"))+
      ylab("Total number of floral units over the four habitats")+
      xlab("Sampling round")+
      labs(col="Plant species")
    AllwithTotal_Species_Change
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-05
      • 1970-01-01
      • 2020-09-19
      • 1970-01-01
      • 2012-02-18
      • 1970-01-01
      • 2019-08-30
      相关资源
      最近更新 更多