在同一图表上绘制不同颜色的矢量的不同部分

时间:2021-04-01 12:02:23

As from the title suppose this vector and plot:

从标题中假设这个向量和图:

plot(rnorm(200,5,2),type="l")

This returns this plot

这将返回此图

在同一图表上绘制不同颜色的矢量的不同部分

What i would like to know is whether there is a way to make the first half of it to be in blue col="blue" and the rest of it to be in red "col="red".

我想知道的是,是否有办法让它的前半部分为蓝色col =“蓝色”,其余部分为红色“col =”red“。

Similar question BUT in Matlab not R: Here

类似的问题但在Matlab而不是R:这里

2 个解决方案

#1


2  

Not a base R solution, but I think this is how to plot it using . It is necessary to prepare a data frame to plot the data.

不是基本R解决方案,但我认为这是如何使用ggplot2绘制它。有必要准备一个数据框来绘制数据。

set.seed(1234)

vec <- rnorm(200,5,2)

dat <- data.frame(Value = vec)
dat$Group <- as.character(rep(c(1, 2), each = 100))
dat$Index <- 1:200

library(ggplot2)

ggplot(dat, aes(x = Index, y = Value)) +
  geom_line(aes(color = Group)) +
  scale_color_manual(values = c("blue", "red")) +
  theme_classic()

在同一图表上绘制不同颜色的矢量的不同部分

We can also use the package with the same data frame.

我们也可以使用具有相同数据帧的网格包。

library(lattice)
xyplot(Value ~ Index, data = dat, type = 'l', groups = Group, col = c("blue", "red"))

在同一图表上绘制不同颜色的矢量的不同部分

Notice that the blue line and red line are disconnected. Not sure if this is important, but if you want to plot a continuous line, here is a workaround in . The idea is to subset the data frame for the second half, plot the entire data frame with color as blue, and then plot the second data frame with color as red.

请注意,蓝线和红线是断开的。不确定这是否重要,但如果你想绘制一条连续线,这里是ggplot2中的一种解决方法。这个想法是为下半部分数据框的子集,用颜色绘制整个数据框为蓝色,然后绘制第二个数据框,颜色为红色。

dat2 <- dat[dat$Index %in% 101:200, ]

ggplot(dat, aes(x = Index, y = Value)) +
  geom_line(color = "blue") +
  geom_line(data = dat2, aes(x = Index, y = Value), color = "red") +
  theme_classic()

在同一图表上绘制不同颜色的矢量的不同部分

#2


2  

You could simply use lines for the second half:

你可以简单地在下半场使用线条:

dat <- rnorm(200, 5, 2)
plot(1:100, dat[1:100], col = "blue", type = "l", xlim = c(0, 200), ylim = c(min(dat), max(dat)))
lines(101:200, dat[101:200], col = "red")

在同一图表上绘制不同颜色的矢量的不同部分

#1


2  

Not a base R solution, but I think this is how to plot it using . It is necessary to prepare a data frame to plot the data.

不是基本R解决方案,但我认为这是如何使用ggplot2绘制它。有必要准备一个数据框来绘制数据。

set.seed(1234)

vec <- rnorm(200,5,2)

dat <- data.frame(Value = vec)
dat$Group <- as.character(rep(c(1, 2), each = 100))
dat$Index <- 1:200

library(ggplot2)

ggplot(dat, aes(x = Index, y = Value)) +
  geom_line(aes(color = Group)) +
  scale_color_manual(values = c("blue", "red")) +
  theme_classic()

在同一图表上绘制不同颜色的矢量的不同部分

We can also use the package with the same data frame.

我们也可以使用具有相同数据帧的网格包。

library(lattice)
xyplot(Value ~ Index, data = dat, type = 'l', groups = Group, col = c("blue", "red"))

在同一图表上绘制不同颜色的矢量的不同部分

Notice that the blue line and red line are disconnected. Not sure if this is important, but if you want to plot a continuous line, here is a workaround in . The idea is to subset the data frame for the second half, plot the entire data frame with color as blue, and then plot the second data frame with color as red.

请注意,蓝线和红线是断开的。不确定这是否重要,但如果你想绘制一条连续线,这里是ggplot2中的一种解决方法。这个想法是为下半部分数据框的子集,用颜色绘制整个数据框为蓝色,然后绘制第二个数据框,颜色为红色。

dat2 <- dat[dat$Index %in% 101:200, ]

ggplot(dat, aes(x = Index, y = Value)) +
  geom_line(color = "blue") +
  geom_line(data = dat2, aes(x = Index, y = Value), color = "red") +
  theme_classic()

在同一图表上绘制不同颜色的矢量的不同部分

#2


2  

You could simply use lines for the second half:

你可以简单地在下半场使用线条:

dat <- rnorm(200, 5, 2)
plot(1:100, dat[1:100], col = "blue", type = "l", xlim = c(0, 200), ylim = c(min(dat), max(dat)))
lines(101:200, dat[101:200], col = "red")

在同一图表上绘制不同颜色的矢量的不同部分