简体   繁体   English

如何使用 ggplot2 在 plot 中的 append 行

[英]How to append lines in a plot with ggplot2

I am trying to make a plot like this in an easy way with ggplot (or base R)我正在尝试使用ggplot(或base R)以一种简单的方式制作像这样的plot

在此处输入图像描述

The idea is to consider the dark line as a single group with a gap between two subgroups (CAP and NC), and also to put a second x lab to tag times (as you can see, times 0,15 and 240 are common to both groups).这个想法是将暗线视为一个组,在两个子组(CAP 和 NC)之间有间隙,并且还放置第二个 x 实验室来标记时间(如您所见,时间 0,15 和 240 是常见的两组)。

Thanks for your help,谢谢你的帮助,

EDIT: Here there is an example.编辑:这里有一个例子。

db0 <- structure(list(t = c(0, 5, 15, 30, 60, 0, 15, 60, 0, 5, 15, 
30,60, 0, 15, 60), g = c("C", "C", "C", "C", "C", "N", "N", "N", 
"C", "C", "C", "C", "C", "N", "N", "N"), v = c("T1", "T1", "T1", 
"T1", "T1", "T1", "T1", "T1", "T2", "T2", "T2", "T2", "T2", "T2", 
"T2", "T2"), y = c(10, 12, 15, 25, 33, 10, 13, 11, 11, 20, 28, 
14, 10, 11, 11, 12), x = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L)), row.names = c(NA, -16L), class = 
"data.frame")

db0$x <- rep(1:8, length.out=16)

library(ggplot2)
ggplot(db0, aes(x=as.factor(x), y=y, group=g, color=v)) + 
  geom_path() + 
  geom_point() +
  scale_x_discrete(labels=c("1" = "0",  "2" = "5", "3" = "15", "4" = "30",
                            "5" = "60", "6" = "0", "7" = "15", "8" = "60"))

在此处输入图像描述

The question here is how to do this plot in an easy way, without creating the x variable and the changing the x-lab names, but with ggplot2 commands.这里的问题是如何以简单的方式执行此 plot,无需创建 x 变量和更改 x-lab 名称,而是使用 ggplot2 命令。 Also, Having problems with geom_path that links the end of one group with the beginning of the following.此外,geom_path 存在问题,它将一组的结尾与以下组的开头联系起来。

  1. Use group = interaction(g, v) to prevent the lines from connecting.使用group = interaction(g, v)来防止线路连接。
  2. I think you can use facet_grid to get similar results.我认为您可以使用facet_grid获得类似的结果。

BTW, I'm using x=t here to get the actual value for the x-axis instead of attempting to map it with ordinals and such.顺便说一句,我在这里使用x=t来获取 x 轴的实际值,而不是尝试用序数等 map 它。

ggplot(db0, aes(x=t, y=y, group=interaction(g, v), color=v)) + 
  geom_path() + 
  geom_point() +
  facet_grid(. ~ g, space = "free_x")

多面 ggplot2 与线断开

I added space="free_x" here to attempt to get the x-axis on the right to compress, which is when I noticed that the x-range is the same for the left and the right... theoretically, if they were different, the size of each pane might be proportional.我在此处添加了space="free_x"以尝试使右侧的 x 轴进行压缩,这时我注意到左侧和右侧的 x 范围相同...理论上,如果它们不同,每个窗格的大小可能是成比例的。 (See https://stackoverflow.com/a/10455381/3358272 ) (见https://stackoverflow.com/a/10455381/3358272

There is a bit of formatting/labeling you'll need to do to get the "CAP" and "NC" labels you seek.您需要进行一些格式化/标记才能获得所需的“CAP”和“NC”标签。

If you need similar x-ranges but dissimilar widths, then you will likely need to munge the x-axis of one of the groups and then attach a labeller somehow that distinguishes between the two groups.如果您需要相似的 x 范围但不同的宽度,那么您可能需要调整其中一个组的 x 轴,然后以某种方式附加一个贴标机来区分两组。

A third option is to produce two separate plots (using different x-axis controls) and use a package like patchwork to combine them with dissimilar widths.第三种选择是生成两个单独的图(使用不同的 x 轴控件)并使用 package 之类的patchwork而成,将它们与不同的宽度组合在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM