我有一个散布的数据点图,并且能够使用abline
创建象限线。现在,我试图为第1象限和第3象限中的点着色。我将首先绘制所有点,然后仅将第1象限中的点绘制为红色,并在第3象限中的点绘制为蓝色。它们将被叠加在旧的上。但是,我不知道如何按象限绘制点。谢谢。
“编写R代码,以红色绘制(waiting,爆发)(Q1,Q1)象限中的点,以蓝色绘制(waiting,爆发)(Q3,Q3)象限中的点,其他所有黑色部分,请使用pch=19
和cex=0.5
(提示,首先使用pch=19, col="black"
绘制所有内容,然后添加条件颜色和所需的颜色,这些条件点将叠加在旧颜色上,而剩下的颜色将保持黑色)。”
这是我的基础:
plot(faithful$waiting, faithful$eruptions, pch=19, cex=0.5, col="black")
我意识到我只需要以一种颜色绘制象限1中的那些,然后以另一种颜色绘制象限3中的那些,然后在我的基础之后运行该代码。但我不确定如何。谢谢。
dput(faithful)
是:structure(list(eruptions = c(3.6, 1.8, 3.333, 2.283, 4.533, 2.883,
4.7, 3.6, 1.95, 4.35, 1.833, 3.917, 4.2, 1.75, 4.7, 2.167, 1.75,
4.8, 1.6, 4.25, 1.8, 1.75, 3.45, 3.067, 4.533, 3.6, 1.967, 4.083,
3.85, 4.433, 4.3, 4.467, 3.367, 4.033, 3.833, 2.017, 1.867, 4.833,
1.833, 4.783, 4.35, 1.883, 4.567, 1.75, 4.533, 3.317, 3.833,
2.1, 4.633, 2, 4.8, 4.716, 1.833, 4.833, 1.733, 4.883, 3.717,
1.667, 4.567, 4.317, 2.233, 4.5, 1.75, 4.8, 1.817, 4.4, 4.167,
4.7, 2.067, 4.7, 4.033, 1.967, 4.5, 4, 1.983, 5.067, 2.017, 4.567,
3.883, 3.6, 4.133, 4.333, 4.1, 2.633, 4.067, 4.933, 3.95, 4.517,
2.167, 4, 2.2, 4.333, 1.867, 4.817, 1.833, 4.3, 4.667, 3.75,
1.867, 4.9, 2.483, 4.367, 2.1, 4.5, 4.05, 1.867, 4.7, 1.783,
4.85, 3.683, 4.733, 2.3, 4.9, 4.417, 1.7, 4.633, 2.317, 4.6,
1.817, 4.417, 2.617, 4.067, 4.25, 1.967, 4.6, 3.767, 1.917, 4.5,
2.267, 4.65, 1.867, 4.167, 2.8, 4.333, 1.833, 4.383, 1.883, 4.933,
2.033, 3.733, 4.233, 2.233, 4.533, 4.817, 4.333, 1.983, 4.633,
2.017, 5.1, 1.8, 5.033, 4, 2.4, 4.6, 3.567, 4, 4.5, 4.083, 1.8,
3.967, 2.2, 4.15, 2, 3.833, 3.5, 4.583, 2.367, 5, 1.933, 4.617,
1.917, 2.083, 4.583, 3.333, 4.167, 4.333, 4.5, 2.417, 4, 4.167,
1.883, 4.583, 4.25, 3.767, 2.033, 4.433, 4.083, 1.833, 4.417,
2.183, 4.8, 1.833, 4.8, 4.1, 3.966, 4.233, 3.5, 4.366, 2.25,
4.667, 2.1, 4.35, 4.133, 1.867, 4.6, 1.783, 4.367, 3.85, 1.933,
4.5, 2.383, 4.7, 1.867, 3.833, 3.417, 4.233, 2.4, 4.8, 2, 4.15,
1.867, 4.267, 1.75, 4.483, 4, 4.117, 4.083, 4.267, 3.917, 4.55,
4.083, 2.417, 4.183, 2.217, 4.45, 1.883, 1.85, 4.283, 3.95, 2.333,
4.15, 2.35, 4.933, 2.9, 4.583, 3.833, 2.083, 4.367, 2.133, 4.35,
2.2, 4.45, 3.567, 4.5, 4.15, 3.817, 3.917, 4.45, 2, 4.283, 4.767,
4.533, 1.85, 4.25, 1.983, 2.25, 4.75, 4.117, 2.15, 4.417, 1.817,
4.467), waiting = c(79, 54, 74, 62, 85, 55, 88, 85, 51, 85, 54,
84, 78, 47, 83, 52, 62, 84, 52, 79, 51, 47, 78, 69, 74, 83, 55,
76, 78, 79, 73, 77, 66, 80, 74, 52, 48, 80, 59, 90, 80, 58, 84,
58, 73, 83, 64, 53, 82, 59, 75, 90, 54, 80, 54, 83, 71, 64, 77,
81, 59, 84, 48, 82, 60, 92, 78, 78, 65, 73, 82, 56, 79, 71, 62,
76, 60, 78, 76, 83, 75, 82, 70, 65, 73, 88, 76, 80, 48, 86, 60,
90, 50, 78, 63, 72, 84, 75, 51, 82, 62, 88, 49, 83, 81, 47, 84,
52, 86, 81, 75, 59, 89, 79, 59, 81, 50, 85, 59, 87, 53, 69, 77,
56, 88, 81, 45, 82, 55, 90, 45, 83, 56, 89, 46, 82, 51, 86, 53,
79, 81, 60, 82, 77, 76, 59, 80, 49, 96, 53, 77, 77, 65, 81, 71,
70, 81, 93, 53, 89, 45, 86, 58, 78, 66, 76, 63, 88, 52, 93, 49,
57, 77, 68, 81, 81, 73, 50, 85, 74, 55, 77, 83, 83, 51, 78, 84,
46, 83, 55, 81, 57, 76, 84, 77, 81, 87, 77, 51, 78, 60, 82, 91,
53, 78, 46, 77, 84, 49, 83, 71, 80, 49, 75, 64, 76, 53, 94, 55,
76, 50, 82, 54, 75, 78, 79, 78, 78, 70, 79, 70, 54, 86, 50, 90,
54, 54, 77, 79, 64, 75, 47, 86, 63, 85, 82, 57, 82, 67, 74, 54,
83, 73, 73, 88, 80, 71, 83, 56, 79, 78, 84, 58, 83, 43, 60, 75,
81, 46, 90, 46, 74)), row.names = c(NA, 272L), class = "data.frame")
以及我如何定义象限:
plot(faithful$waiting, faithful$eruptions, pch=19, cex=0.5, col="green")
abline(v=quantile(faithful$waiting, 0.75), col="red")
abline(h=quantile(faithful$eruptions,0.75),col="red")
abline(v=quantile(faithful$waiting, 0.25), col="red")
abline(h=quantile(faithful$eruptions,0.25),col="red")
mtext("Q1", side=1, at=quantile(faithful$waiting, 0.25), col="red")
mtext("Q1", side=2, at=quantile(faithful$eruptions, 0.25), col="red")
mtext("Q3", side=2, at=quantile(faithful$eruptions, 0.75), col="red")
mtext("Q3", side=1, at=quantile(faithful$waiting, 0.75), col="red")
使用分组时我一直在尝试的方法:
faithful$color<- 'black'
q1<-quantile(faithful$waiting, 0.25) #how do I assign the quantiles
q3<-quantile(faithful$waiting, 0.75)
faithful$color[faithful$waiting ]="red" #how do I then assign the quantiles a color
faithful$color[faithful$waiting ]="blue"
最佳答案
我们可以将两个变量cut
到分位数中,然后将级别paste
在一起,以获得每个象限的组。
faithful <- within(faithful, {
e.qtl <- as.integer(cut(faithful$eruptions,
quantile(faithful$eruptions, c(0, .25, .75, 1)),
include.lowest=TRUE))
w.qtl <- as.integer(cut(faithful$waiting,
quantile(faithful$waiting, c(0, .25, .75, 1)),
include.lowest=TRUE))
qtl <- as.integer(as.factor(with(faithful,
paste0(e.qtl, w.qtl))))
})
然后,我们可以轻松地绘制分配
col
或每个组的数据。colors <- rainbow(length(unique(faithful$qtl))) # create colors
# plot
plot(faithful$waiting, faithful$eruptions, type="n") # empty plot here
with(faithful, points(waiting, eruptions, col=colors[qtl], pch=19, cex=0.5))
之后,添加
abline
和mtext
。abline(v=quantile(faithful$waiting, 0.75), col="red")
abline(h=quantile(faithful$eruptions, 0.75),col="red")
abline(v=quantile(faithful$waiting, 0.25), col="red")
abline(h=quantile(faithful$eruptions, 0.25),col="red")
mtext("Q1", side=1, at=quantile(faithful$waiting, 0.25), col="red")
mtext("Q1", side=2, at=quantile(faithful$eruptions, 0.25), col="red")
mtext("Q3", side=2, at=quantile(faithful$eruptions, 0.75), col="red")
mtext("Q3", side=1, at=quantile(faithful$waiting, 0.75), col="red")
结果
关于r - 如何更改特定象限中点的颜色?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55678191/