尝试按照下图所示的方式为曲线着色(以乳胶中的pgfplots为例)。 这可能在R? 这是我的数据:
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)
答案 0 :(得分:4)
这是基础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))
如果你想在其他解决方案的评论中硬编码颜色,那么事情就更容易了
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)
答案 1 :(得分:2)
我想你可能正在寻找这个颜色并在同一轴上绘图...
我已经在x_ vs y _
上着色并绘制library('ggplot2')
df <- data.frame(x_,y_)
ggplot(df,aes(x_,y_,color=x_)) + geom_line()
答案 2 :(得分:2)
这是一种基本图形方法。您需要对所需的颜色大惊小怪,最终可能会将x_
和y_
放入数据框并对其进行排序,以便颜色可以随着x轴的增加而平滑变化。但是,让我们看看这是否与您的想法相近。
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)
评论中的每个细节更新:
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])
产地: