R 仅在指定因素之间连接几何线

R 仅在指定因素之间连接几何线,r,ggplot2,plot,R,Ggplot2,Plot,我有一个数据集,其中有4个治疗组几个月的直径值。我正在为每个月绘制Diameter~Treatment,以及直径在每个月~Treatment之间的变化 数据集如下所示: # the data that contains diameter for each month and diameter differences between months > head(gatheredDiameterAndTreatmentData) Treatment Month Diam

我有一个数据集,其中有4个治疗组几个月的直径值。我正在为每个月绘制
Diameter~Treatment
,以及
直径在每个月~Treatment
之间的变化

数据集如下所示:

# the data that contains diameter for each month and diameter differences between months > head(gatheredDiameterAndTreatmentData) Treatment Month Diameter 1 Aux_Drop Diameter_mm.Sep01 55.88 2 Aux_Spray Diameter_mm.Sep01 63.50 3 DMSO Diameter_mm.Sep01 66.04 4 Water Diameter_mm.Sep01 43.18 5 Aux_Drop Diameter_mm.Sep01 38.10 6 Aux_Spray Diameter_mm.Sep01 76.20 # data that contains mean diameter and mean diameter changes for each month > head(subMeansDiameter) Treatment Month Diameter SEdiam 1 Aux_Drop Diameter_mm.Dec 83.63857 29.62901 2 Aux_Drop Diameter_mm.Feb01 101.20923 24.84024 3 Aux_Drop Diameter_mm.Feb02 110.00154 22.51364 4 Aux_Drop Diameter_mm.Jan 93.00308 25.13485 5 Aux_Drop Diameter_mm.Mar 116.84000 22.19171 6 Aux_Drop Diameter_mm.Nov01 74.50667 17.40454
# assign the factors name to pick
factorsOnXaxis.DiameterByMonth = c(
    "Diameter_mm.Sep01", "DiameterDiff.Sep01ToDec", "Diameter_mm.Dec", "DiameterDiff.DecToMar", "Diameter_mm.Mar")

# assign name to above factors
factorsOnXaxisName = c('Sep','Dec-Sep','Dec', 'Mar-Dec', 'Mar')    


# start plotting 
gatheredDiameterAndTreatmentData  %>%
  subset(Diameter != "NA") %>%
  ggplot(aes(x = factor(Month), y = Diameter)) + 
  geom_point(aes(colour = Treatment), na.rm = TRUE, 
             position = position_dodge(width = 0.2)) +
  geom_point(data = subMeansDiameter, size = 4, aes(colour = Treatment), 
             na.rm = TRUE, position = position_dodge(width = 0.2)) +

  theme_bw() + # remove background 

  # add custom color to the "Treatment" levels 
  scale_colour_manual( 
    values = c("Aux_Drop" = "Purple", "Aux_Spray" = "Red", 
               "DMSO" = "Orange", "Water" = "Green")) + 

  # rearrange the x-axis
  scale_x_discrete(limits = factorsOnXaxis.DiameterByMonth, labels = factorsOnXaxisName) +

  # to connect the "subMeans - Diameter" values across time points
  geom_line(data = subMeansDiameter, aes(
    x = Month, y = Diameter, group = Treatment, colour = Treatment), 
    position = position_dodge(width = 0.2)) 
geom_line(data = subMeansDiameter, aes( x = c("DiameterDiff.Sep01ToDec", "DiameterDiff.DecToMar"), y = Diameter, group = Treatment, colour = Treatment), position = position_dodge(width = 0.2)) 这给了我这样一个情节:

我希望在指定的x轴因子之间连接线,而不是每个时间点的
geom_线
连接线,即

  • 九月、十二月、三月之间
  • 12月至9月至3月至12月

  • 我试图操纵使用
    geom\u行的代码行:

    # the data that contains diameter for each month and diameter differences between months > head(gatheredDiameterAndTreatmentData) Treatment Month Diameter 1 Aux_Drop Diameter_mm.Sep01 55.88 2 Aux_Spray Diameter_mm.Sep01 63.50 3 DMSO Diameter_mm.Sep01 66.04 4 Water Diameter_mm.Sep01 43.18 5 Aux_Drop Diameter_mm.Sep01 38.10 6 Aux_Spray Diameter_mm.Sep01 76.20 # data that contains mean diameter and mean diameter changes for each month > head(subMeansDiameter) Treatment Month Diameter SEdiam 1 Aux_Drop Diameter_mm.Dec 83.63857 29.62901 2 Aux_Drop Diameter_mm.Feb01 101.20923 24.84024 3 Aux_Drop Diameter_mm.Feb02 110.00154 22.51364 4 Aux_Drop Diameter_mm.Jan 93.00308 25.13485 5 Aux_Drop Diameter_mm.Mar 116.84000 22.19171 6 Aux_Drop Diameter_mm.Nov01 74.50667 17.40454
    # assign the factors name to pick
    factorsOnXaxis.DiameterByMonth = c(
        "Diameter_mm.Sep01", "DiameterDiff.Sep01ToDec", "Diameter_mm.Dec", "DiameterDiff.DecToMar", "Diameter_mm.Mar")
    
    # assign name to above factors
    factorsOnXaxisName = c('Sep','Dec-Sep','Dec', 'Mar-Dec', 'Mar')    
    
    
    # start plotting 
    gatheredDiameterAndTreatmentData  %>%
      subset(Diameter != "NA") %>%
      ggplot(aes(x = factor(Month), y = Diameter)) + 
      geom_point(aes(colour = Treatment), na.rm = TRUE, 
                 position = position_dodge(width = 0.2)) +
      geom_point(data = subMeansDiameter, size = 4, aes(colour = Treatment), 
                 na.rm = TRUE, position = position_dodge(width = 0.2)) +
    
      theme_bw() + # remove background 
    
      # add custom color to the "Treatment" levels 
      scale_colour_manual( 
        values = c("Aux_Drop" = "Purple", "Aux_Spray" = "Red", 
                   "DMSO" = "Orange", "Water" = "Green")) + 
    
      # rearrange the x-axis
      scale_x_discrete(limits = factorsOnXaxis.DiameterByMonth, labels = factorsOnXaxisName) +
    
      # to connect the "subMeans - Diameter" values across time points
      geom_line(data = subMeansDiameter, aes(
        x = Month, y = Diameter, group = Treatment, colour = Treatment), 
        position = position_dodge(width = 0.2)) 
    
    geom_line(data = subMeansDiameter, aes( x = c("DiameterDiff.Sep01ToDec", "DiameterDiff.DecToMar"), y = Diameter, group = Treatment, colour = Treatment), position = position_dodge(width = 0.2)) 几何尺寸线(数据=直径,aes( x=c(“DiameterDiff.Sep01ToDec”、“DiameterDiff.DecToMar”)、y=DiameterDiff.DecToMar、y=Diameter、group=TREAMED、COLOURE=TREAMED), 位置=位置\减淡(宽度=0.2)) 将
    Dec-Sep
    Mar-Dec
    之间的线路连接起来

    但是,这是行不通的。如何更改代码

    这是我存储为*.tsv的数据文件。

    收集的直径和治疗数据=


    SubMans=

    这里您需要明确定义组,因为颜色不够

    你的例子是不可复制的,但这里有一些东西可以让你产生想法,这是一个没有明确分组的图:

    ggplot(iris,aes(Sepal.Width, Sepal.Length, color = Species)) + geom_line()
    

    现在,这里有一个组美学,我使用
    Sepal.Length
    的值分割数据,但您最有可能使用
    ifelse
    在月份终止:

    ggplot(iris,aes(Sepal.Width, Sepal.Length, color = Species, 
                    group = interaction(Species, Sepal.Length > 5.5))) + 
      geom_line()
    

    添加一个组aesthetic@Moody_Mudskipper:请提供代码。谢谢您的回答。我看到了另一个使用交互的例子。我必须检查如何应用
    交互
    方法。顺便说一句,我上传了整个数据作为文本文件,并与问题分享了它。你现在可以看一下吗?交互为每个现有的组合建立一个唯一的id,想法是为每个颜色组合(即物种)建立一个组,并且“是否>5.5”我得到“未找到”,我建议你制作一个简洁的可复制示例,不需要外部数据,或者问任何你对目前答案不了解的问题。