R:Smooth 为值创建特定颜色的散点图
R:Smooth scatter plot creating specific color for values
你好,我已经创建了基于数据类别着色的平滑散点图,现在我没有得到哪种颜色代表哪个类别所以为了理解如果我将特定颜色传递给每个类别它看起来会更方便根据如何我这样做 col=df$cat
它不是属于它的颜色特定点 我不理解任何人建议我
每个类别的特定颜色,例如 data1=green,data2=blue,data3=black,data4=red
我的数据
A B cat
0.8803 0.0342 data1
0.9174 0.0331 data1
0.9083 0.05 data1
0.7542 0.161 data2
0.8983 0.0593 data2
0.8182 0.1074 data2
0.3525 0.3525 data3
0.5339 0.2288 data3
0.7295 0.082 data3
0.8 0.0417 data4
0.8291 0.0598 data4
0.8 0.025 data4
我试过的脚本
df=read.table("test.txt", sep='\t', header=TRUE)
smoothScatter(df$B,df$A,,nrpoints=Inf,xlim=c(0,1),ylim=c(0,1), pch=20,cex=1, col=df$cat)
points(c(0,1),c(1,0),type='l',col='green',lty=2,lwd=2)
legend(0.30, 1, c("diagonal"),
col = c("green"),
lty = 2, lwd = 4)
谢谢
首先将颜色向量创建为命名向量。然后使用 x=A
、y=B
绘制,并且仅在 geom_point
中绘制 color=cat
。对角线由 geom_line
调用绘制,带有新的 data
参数,即直线的终点。
library(ggplot2)
color <- c(data1="green", data2="blue", data3="black", data4="red")
ggplot(df, aes(A, B)) +
geom_point(aes(color = cat), alpha = 0.25) +
geom_line(
data = data.frame(A = 0:1, B = 1:0),
mapping = aes(A, B),
color = "green",
linetype = "dashed"
) +
scale_color_manual(values = color) +
theme_bw()
基础 R
与smoothScatter
的关系图如下。
colnum <- match(df$cat, names(color))
smoothScatter(
df$B, df$A,
nrpoints = Inf,
xlim = c(0, 1), ylim = c(0, 1),
pch = 20, cex = 1,
col = color[colnum]
)
points(c(0, 1), c(1, 0), type = 'l', col = 'green', lty = 2, lwd = 2)
legend(0.30, 1, "diagonal", col = "green", lty = 2, lwd = 4)
或者,如果我们删除 nrpoints = Inf
smoothScatter(
df$B, df$A,
#nrpoints = Inf,
xlim = c(0, 1), ylim = c(0, 1),
pch = 20, cex = 1,
col = color[colnum]
)
points(c(0, 1), c(1, 0), type = 'l', col = 'green', lty = 2, lwd = 2)
legend(0.30, 1, "diagonal", col = "green", lty = 2, lwd = 4)
数据
google_id <- "1FN0H7ilY9OwcQC7Y8iiLUZd2CTuJ9U4Z"
google_file <- sprintf("https://docs.google.com/uc?id=%s&export=download", google_id)
df <- read.table(google_file, header = TRUE)
你好,我已经创建了基于数据类别着色的平滑散点图,现在我没有得到哪种颜色代表哪个类别所以为了理解如果我将特定颜色传递给每个类别它看起来会更方便根据如何我这样做 col=df$cat
它不是属于它的颜色特定点 我不理解任何人建议我
每个类别的特定颜色,例如 data1=green,data2=blue,data3=black,data4=red
我的数据
A B cat
0.8803 0.0342 data1
0.9174 0.0331 data1
0.9083 0.05 data1
0.7542 0.161 data2
0.8983 0.0593 data2
0.8182 0.1074 data2
0.3525 0.3525 data3
0.5339 0.2288 data3
0.7295 0.082 data3
0.8 0.0417 data4
0.8291 0.0598 data4
0.8 0.025 data4
我试过的脚本
df=read.table("test.txt", sep='\t', header=TRUE)
smoothScatter(df$B,df$A,,nrpoints=Inf,xlim=c(0,1),ylim=c(0,1), pch=20,cex=1, col=df$cat)
points(c(0,1),c(1,0),type='l',col='green',lty=2,lwd=2)
legend(0.30, 1, c("diagonal"),
col = c("green"),
lty = 2, lwd = 4)
谢谢
首先将颜色向量创建为命名向量。然后使用 x=A
、y=B
绘制,并且仅在 geom_point
中绘制 color=cat
。对角线由 geom_line
调用绘制,带有新的 data
参数,即直线的终点。
library(ggplot2)
color <- c(data1="green", data2="blue", data3="black", data4="red")
ggplot(df, aes(A, B)) +
geom_point(aes(color = cat), alpha = 0.25) +
geom_line(
data = data.frame(A = 0:1, B = 1:0),
mapping = aes(A, B),
color = "green",
linetype = "dashed"
) +
scale_color_manual(values = color) +
theme_bw()
基础 R
与smoothScatter
的关系图如下。
colnum <- match(df$cat, names(color))
smoothScatter(
df$B, df$A,
nrpoints = Inf,
xlim = c(0, 1), ylim = c(0, 1),
pch = 20, cex = 1,
col = color[colnum]
)
points(c(0, 1), c(1, 0), type = 'l', col = 'green', lty = 2, lwd = 2)
legend(0.30, 1, "diagonal", col = "green", lty = 2, lwd = 4)
或者,如果我们删除 nrpoints = Inf
smoothScatter(
df$B, df$A,
#nrpoints = Inf,
xlim = c(0, 1), ylim = c(0, 1),
pch = 20, cex = 1,
col = color[colnum]
)
points(c(0, 1), c(1, 0), type = 'l', col = 'green', lty = 2, lwd = 2)
legend(0.30, 1, "diagonal", col = "green", lty = 2, lwd = 4)
数据
google_id <- "1FN0H7ilY9OwcQC7Y8iiLUZd2CTuJ9U4Z"
google_file <- sprintf("https://docs.google.com/uc?id=%s&export=download", google_id)
df <- read.table(google_file, header = TRUE)