仅在指定因素之间连接geom_line [英] Connect geom_line only between specified factors

查看:40
本文介绍了仅在指定因素之间连接geom_line的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,其中包含四个不同月份的四个治疗组的直径值.我正在绘制每个月的 Diameter〜Treatment ,以及每个月〜Treatment 之间的 Diameter变化.

数据集如下:

#包含每月直径和月份之间直径差异的数据>头(gatheredDiameterAndTreatmentData)治疗月直径1 Aux_Drop直径_mm.Sep01 55.882 Aux_Spray直径_mm.Sep01 63.503 DMSO直径_毫米.Sep01 66.044水直径_mm.Sep01 43.185 Aux_Drop直径_mm.Sep01 38.106辅助喷雾直径_mm.Sep01 76.20#包含平均直径和每个月平均直径变化的数据>头(subMeansDiameter)治疗月份直径SEdiam1 Aux_Drop直径_mm.Dec 83.63857 29.629012 Aux_Drop直径_mm.Feb01 101.20923 24.840243 Aux_Drop直径_mm.Feb02 110.00154 22.513644 Aux_Drop直径_mm 1月93.00308 25.134855 Aux_Drop Diameter_mm.Mar 116.84000 22.191716 Aux_Drop直径_mm.Nov01 74.50667 17.40454


这是我的代码:

 #分配要选择的因子名称FactorsOnXaxis.DiameterByMonth = c("Diameter_mm.Sep01","DiameterDiff.Sep01ToDec","Diameter_mm.Dec","DiameterDiff.DecToMar","Diameter_mm.Mar")#为上述因素分配名称FactorsOnXaxisName = c('Sep','Dec-Sep','Dec','Mar-Dec','Mar')#开始绘图collectDiameterAndTreatmentData%&%;%子集(直径!="NA")%>%ggplot(aes(x =因子(月),y =直径))+geom_point(aes(colour = Treatment),na.rm = TRUE,位置= position_dodge(width = 0.2))+geom_point(数据= subMeansDiameter,大小= 4,aes(颜色=处理),na.rm = TRUE,位置= position_dodge(width = 0.2))+theme_bw()+#删除背景#将自定义颜色添加到处理"级别scale_colour_manual(值= c("Aux_Drop" =紫色","Aux_Spray" =红色","DMSO" =橙色",水" =绿色"))+#重新排列x轴scale_x_discrete(限制= factorsOnXaxis.DiameterByMonth,标签= factorsOnXaxisName)+#连接跨时间点的"subMeans-直径"值geom_line(data = subMeansDiameter,aes(x =月,y =直径,组=处理,颜色=处理),位置= position_dodge(宽度= 0.2)) 

哪个给了我这样的情节:

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

  1. 在9月,12月,3月之间
  2. 十二月至九月的三月


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

geom_line(data = subMeansDiameter,aes(x = c("DiameterDiff.Sep01ToDec","DiameterDiff.DecToMar"),y =直径,组=处理,颜色=处理),位置= position_dodge(宽度= 0.2))

Dec-Sep Mar-Dec 之间的线连接起来.

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

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

gatheredDiameterAndTreatmentData =

现在这是一个具有整体美感的模型,我已经使用 Sepal.Length 的值拆分了数据,但是您很可能会使用 ifelse 月:

  ggplot(iris,aes(Sepal.Width,Sepal.Length,color = Species,组=相互作用(Species,Sepal.Length> 5.5)))+geom_line() 

I have a dataset that has diameter values for 4 treatment groups for several different months. I am plotting Diameter ~ Treatment for each month, as well as the Diameter changes between months ~ Treatment.

Dataset looks like this:

# 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


Here is my code:

# 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)) 

Which gives me a plot like this:

Instead of geom_line connecting line for each time points I want the line to be joined between specified x-axis factors, i.e

  1. between Sep, Dec, March
  2. between Dec-Sep to Mar-Dec


I tried to manipulate the code line that uses geom_line as:

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

to connect the line between Dec-Sep to Mar-Dec.

But, this is not working. How can I change my code?

Here is the data file I stores as *.tsv.

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

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

解决方案

Here you need to define groups explicitly as color is not enough.

Your example is not reproducible but here's something that will give you the idea, here's a plot with no explicit group:

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

And now here's one with a group aesthetic, I have split the data using Sepal.Length's values but you'll most likely use an ifelse deending on the month :

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

这篇关于仅在指定因素之间连接geom_line的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆