r - 仅在指定因素之间连接 geom_line

标签 r ggplot2 plot

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

数据集如下所示:

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

这给了我这样的情节:

enter image description here

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

  1. 九月、十二月、三月之间
  2. 12 月至 9 月至 3 月至 12 月


我尝试将使用 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

子平均值 = http://s000.tinyupload.com/index.php?file_id=93947954496987393129

最佳答案

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

你的例子是不可重现的,但这里有一些东西可以给你这个想法,这是一个没有明确组的情节:

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

关于r - 仅在指定因素之间连接 geom_line,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52691691/

相关文章:

c - 将 R CMD SHLIB 与 OpenMP 一起使用不用于包构建

R ggplot + ggplotly : color scale doesn't work

R 在最大值和最小值处绘制 y 轴刻度条

r - 更改 R 方图中的标签位置(决策/回归树)

r - 获取r中具有最大日期的所有相应行

r - 有没有办法用 R 求 pbinom 的概率 p

r - 根据R中字符串的结尾创建新列

r - ggplot2 中的 geoms 和 stats 有什么区别?

r - 如何在 ggplot2 中绘制具有正确间隔的连续 x 轴值的箱线图

python - 从下拉菜单或按钮-Python/Plotly 将 sqrt 设置为 yaxis 比例