[英]How to draw multiple graphs by having same axis but changing x-axis forward with R?
I want to plot two different pairs but with the same x-axis but once the plot is drawn in R, it does not change the x-axis for the next one. 我想绘制两个不同的对,但具有相同的x轴,但是一旦在R中绘制了该图,它就不会更改下一对的x轴。 For example, I want to have both (x,y) pair and (a,b) pair in the same plot.
例如,我想在同一图中同时具有(x,y)对和(a,b)对。 I write:
我写:
x <- c(1,2,3,4,5,6,7)
y <- c(5,3,2,10,8,6,1)
a <- c(4,5,6,7,8,9,10)
b <- c(4,5,8,12,2,6,11)
plot(x,y , type= "l")
lines(a,b)
and it gives me this pic: 它给了我这张照片:
I want it to draw a plot with x-axis from 1 to 10 and y-axis from 2 to 11. This is a simplified example, and in reality, I want to have around five different density plots in the same plot in which the x-axis do not cover each other in some places. 我希望它绘制一个x轴从1到10且y轴从2到11的图。这是一个简化的示例,实际上,我想在同一图中有大约五个不同的密度图,其中x轴在某些地方不会互相覆盖。 I will be appreciated if you explain that this may change the solution or no.
如果您解释说这可能会改变解决方案或不改变,将不胜感激。 Also, I can't use ggplot2 because the dataset is not a panel with group variable available.
另外,我不能使用ggplot2,因为数据集不是具有可用组变量的面板。 It is only several pairs of points.
这只是几对点。
I would strongly advise rethinking the general data structure; 我强烈建议重新考虑一般的数据结构; it is advisable to store data in eg a
list
of data.frame
s. 建议将数据存储在例如
data.frame
list
中。
For example let's consider the following list
based on your sample data. 例如,让我们根据示例数据考虑以下
list
。
lst <- list(
one = data.frame(x = x, y = y),
two = data.frame(x = a, y = b))
We can then plot data using a tidyverse
approach: 然后,我们可以使用
tidyverse
方法绘制数据:
library(tidyverse);
lst %>%
bind_rows(.id = "id") %>%
ggplot(aes(x, y, group = id)) + geom_line()
Or using facets with free x
scales: 或使用具有免费
x
比例的构面:
lst %>%
bind_rows(.id = "id") %>%
ggplot(aes(x, y, group = id)) + geom_line() + facet_wrap(~id, scales = "free_x")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.