简体   繁体   English

r中变量相关的曲线着色

[英]variable dependent curve colouring in r

I'm trying to color a curve in the way shown in the picture below (taken from pgfplots in latex as an example). 我正在尝试以下图所示的方式为曲线着色(以乳胶中的pgfplots为例)。 Is this possible in R ? 在R中这可能吗? Here's my data: 这是我的数据:

x_<-c(1100.9,1115.11,1129.69,1144.65,1160.01,1175.8,1192.01,1208.68,1225.83,1243.47,1261.62,1280.31,1299.56,1319.4,1339.86,1360.96,1382.74,1405.22,1428.45,1452.45,1477.28,1502.97,1529.57,1557.13,1585.71,1615.34,1646.11,1678.08,1711.31,1745.88,1781.88,1819.39)
y_<-c(0.027051452,0.026985964,0.024810857,0.014637821,0.028026167,0.036084976,0.035697714,0.036043107,0.033215440,0.028456798,0.023681321,0.019194500,0.014893107,0.010655131,0.008085667,0.022546167,-0.037710679,-0.231425012,0.109771131,0.117253012,0.033196619,-0.061077119,-0.099846762,-0.079793119,-0.052351238,-0.040228690,-0.040600833,-0.042391202,-0.032053583,-0.022693369,0.017990536,0.090671262)

例

Here is another solution with base R graphics (as opposed to ggplot): 这是使用基本R图形(与ggplot相对)的另一种解决方案:

library("plotrix")
library("colorRamps")

x<-c(1100.9,1115.11,1129.69,1144.65,1160.01,1175.8,1192.01,1208.68,1225.83,1243.47,1261.62,1280.31,1299.56,1319.4,1339.86,1360.96,1382.74,1405.22,1428.45,1452.45,1477.28,1502.97,1529.57,1557.13,1585.71,1615.34,1646.11,1678.08,1711.31,1745.88,1781.88,1819.39)


y<-c(0.027051452,0.026985964,0.024810857,0.014637821,0.028026167,0.036084976,0.035697714,0.036043107,0.033215440,0.028456798,0.023681321,0.019194500,0.014893107,0.010655131,0.008085667,0.022546167,-0.037710679,-0.231425012,0.109771131,0.117253012,0.033196619,-0.061077119,-0.099846762,-0.079793119,-0.052351238,-0.040228690,-0.040600833,-0.042391202,-0.032053583,-0.022693369,0.017990536,0.090671262)

jet.colors <-colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan","#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000"))

plot(x,y,  col=rep(jet.colors(16), each=2), pch=16)
color.scale.lines(x,y, lwd=6, col=rep(jet.colors(16), each=2))

在此处输入图片说明

If you want the colors hard coded as in the comment of the other solution things are easier 如果您希望像其他解决方案的注释一样对颜色进行硬编码,则事情会更容易

colors.mi<-rep(NA, 32)
colors.mi[x<1300]<-"red"
colors.mi[x>1500]<-"green"
colors.mi[is.na(colors.mi)]<-"blue"

plot(x,y,  col=colors.mi, pch=16)
color.scale.lines(x,y, lwd=6, col=colors.mi)

在此处输入图片说明

I think you may be looking for this to color and plot on the same axis... 我想您可能正在寻找在同一个轴上进行着色和绘图的方法...

I have colored and plotted on the x_ vs y_ 我已经在x_和y_上绘制了颜色并进行了绘制

library('ggplot2')
df <- data.frame(x_,y_)
ggplot(df,aes(x_,y_,color=x_)) + geom_line()

这个

Here's a base graphics approach. 这是基本的图形方法。 You'll need to fuss with the colors you want, and probably end up putting x_ and y_ into a data frame and sorting them so that the colors can smoothly change as the x axis increases. 您将需要对所需的颜色进行大惊小怪,并且最终可能会将x_y_放入数据框中并对它们进行排序,以便颜色可以随着x轴的增加而平滑地变化。 But let's see if this is close to what you had in mind. 但是,让我们看看这是否接近您的想法。

col1 <- rev(rainbow(5, start = 0.0, end = 0.25))
col2 <- rev(rainbow(4, start = 0.45, end = 0.66))
myc <- c(col2, col1)

np <- length(x_)
ind1 <- 1:(np-1)
ind2 <- 2:np
plot(x_, y_, type = "n")
segments(x_[ind1], y_[ind1], x_[ind2], y_[ind2], col = myc)

UPDATED per details in comments: 注释中每个细节的更新:

df <- data.frame(x = x_, y = y_)
library('plyr')
df <- arrange(df, x)

# Here's a more automated approach as an example
# col1 <- rev(rainbow(5, start = 0.0, end = 0.25))
# col2 <- rev(rainbow(4, start = 0.45, end = 0.66))
# myc <- c(col2, col1)
# divide x axis into 9 intervals and associate color
# xint <- seq(df$x[1], df$x[length(df$x)], length.out = 9)
# icol <- findInterval(df$x, xint)

# Requested hard-coded version:
xint <- c(1100, 1300, 1500, 1800)
myc <- c("red", "blue", "green")
icol <- findInterval(df$x, xint)

# now plot
np <- length(df$x)
ind1 <- 1:(np-1)
ind2 <- 2:np
plot(df$x, df$y, type = "n")
segments(df$x[ind1], df$y[ind1], df$x[ind2], df$y[ind2], col = myc[icol])

Produces: 产生: 在此处输入图片说明

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

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