仅在指定因素之间连接geom_line

时间:2018-10-07 18:40:28

标签: r ggplot2 plot

我有一个数据集,其中包含4个治疗组在不同月份的直径值。我将每月绘制Diameter ~ Treatment以及Diameter changes between months ~ 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)) 

哪个给了我这样的情节:

enter image description here

我希望将线连接到指定的x轴因数(即

)之间,而不是每个时间点都使用geom_line连接线
  1. 在9月,12月,3月之间
  2. 十二月至九月的三月


我试图操纵使用geom_line的代码行:

geom_line(data = subMeansDiameter, aes(
    x = c("DiameterDiff.Sep01ToDec", "DiameterDiff.DecToMar"), y = Diameter, group = Treatment, colour = Treatment), 
    position = position_dodge(width = 0.2))

Dec-SepMar-Dec之间的线连接起来。

但是,这不起作用。如何更改我的代码?

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

gatheredDiameterAndTreatmentData = http://s000.tinyupload.com/index.php?file_id=38251290073324236098

subMeans = http://s000.tinyupload.com/index.php?file_id=93947954496987393129

1 个答案:

答案 0 :(得分:2)

由于颜色不足,您需要在此处明确定义组。

您的示例不可复制,但是以下内容可以为您提供想法,这是一个没有明确分组的情节:

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

enter image description here

现在这是一种具有整体美感的视图,我已经使用Sepal.Length的值拆分了数据,但是您很可能会使用ifelse来表示月份:

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

enter image description here