R中图的着色点

时间:2021-03-03 15:00:37

I'm trying to colour certain points of a scatter plot in R using logical arguments. My data is a function of sex( M/F ) and faculty (A/M/S/E). I want to colour each faculty, say green, red, blue, black, and then have the females showing as outline only, with a white center.

我正在尝试使用逻辑参数为R中的散点图的某些点着色。我的数据是性别(M / F)和教师(A / M / S / E)的函数。我想给每个教师上色,比如绿色,红色,蓝色,黑色,然后让女性只显示轮廓,白色中心。

plot(year, logit.prop, type="n", xlab="Year of graduation", ylab="Logit proportion of survivors", ylim=c(-1,3))
points(year[faculty=="M"], logit.prop[faculty=="M"], pch=21, 
points(year[faculty=="A"], logit.prop[faculty=="A"], pch=21, bg="red", col="red")

This is a section of the code i'm currently using, is there a way to tell R to colour points where the x axis value (thats year here) is both female and A?

这是我正在使用的代码的一部分,有没有办法告诉R到x轴值(那里的年份)是女性和A的色点?

1 个解决方案

#1


1  

As the @mr.joshuagordon said, col and other arguments to points can take vectors. Since you didn't provide data, here's something:

正如@ mr.joshuagordon所说,col和其他点的论点可以采用向量。由于您没有提供数据,这里有一些东西:

df <- data.frame(x = rep(1:4, each = 2), y = rep(1:2, times = 4), faculty = rep(c("A", "M", "S", "E"), each = 2), gender = rep(c("M","F"), times = 4))
df
#   x y faculty gender
# 1 1 1       A      M
# 2 1 2       A      F
# 3 2 1       M      M
# 4 2 2       M      F
# 5 3 1       S      M
# 6 3 2       S      F
# 7 4 1       E      M
# 8 4 2       E      F
plot(y ~ x, data = df, pch = ifelse(gender == "M", 16, 21), col = factor(faculty))
text(y + 0.5 ~ x, data = df, labels = faculty)

R中图的着色点

In this case, factor(faculty) worked because factors are internally integers counting from 1 on up. You can see what colors are set for each number with:

在这种情况下,因子(faculty)起作用,因为因子是从1开始计数的内部整数。您可以看到为每个数字设置的颜色:

palette()
# [1] "black"   "red"     "green3"  "blue"    "cyan"    "magenta" "yellow" 
# [8] "gray"   

If you want different colors, you can either use conditionals (such as ifelse) on faculty itself, or you can just define the palette colors (see ?palette).

如果你想要不同的颜色,你可以在教师本身使用条件(如ifelse),或者你可以只定义调色板颜色(参见?调色板)。

#1


1  

As the @mr.joshuagordon said, col and other arguments to points can take vectors. Since you didn't provide data, here's something:

正如@ mr.joshuagordon所说,col和其他点的论点可以采用向量。由于您没有提供数据,这里有一些东西:

df <- data.frame(x = rep(1:4, each = 2), y = rep(1:2, times = 4), faculty = rep(c("A", "M", "S", "E"), each = 2), gender = rep(c("M","F"), times = 4))
df
#   x y faculty gender
# 1 1 1       A      M
# 2 1 2       A      F
# 3 2 1       M      M
# 4 2 2       M      F
# 5 3 1       S      M
# 6 3 2       S      F
# 7 4 1       E      M
# 8 4 2       E      F
plot(y ~ x, data = df, pch = ifelse(gender == "M", 16, 21), col = factor(faculty))
text(y + 0.5 ~ x, data = df, labels = faculty)

R中图的着色点

In this case, factor(faculty) worked because factors are internally integers counting from 1 on up. You can see what colors are set for each number with:

在这种情况下,因子(faculty)起作用,因为因子是从1开始计数的内部整数。您可以看到为每个数字设置的颜色:

palette()
# [1] "black"   "red"     "green3"  "blue"    "cyan"    "magenta" "yellow" 
# [8] "gray"   

If you want different colors, you can either use conditionals (such as ifelse) on faculty itself, or you can just define the palette colors (see ?palette).

如果你想要不同的颜色,你可以在教师本身使用条件(如ifelse),或者你可以只定义调色板颜色(参见?调色板)。