R:平滑散点图,为值创建特定颜色
您好,我已经创建了基于数据类别的彩色平滑散点图,现在我不知道哪种颜色代表哪一个类别,因此为了理解,如果我将特定颜色传递给每个类别,它将看起来更方便,根据这一点,我如何才能做到这一点R:平滑散点图,为值创建特定颜色,r,plot,scatter-plot,R,Plot,Scatter Plot,您好,我已经创建了基于数据类别的彩色平滑散点图,现在我不知道哪种颜色代表哪一个类别,因此为了理解,如果我将特定颜色传递给每个类别,它将看起来更方便,根据这一点,我如何才能做到这一点col=df$cat它所属的非特定颜色点我不理解任何人建议我 每个类别的特定颜色,例如data1=绿色、data2=蓝色、data3=黑色、data4=红色 我的数据 A B cat 0.8803 0.0342 data1 0.9174 0.0331 data1 0.9083 0.05
col=df$cat
它所属的非特定颜色点我不理解任何人建议我
每个类别的特定颜色,例如data1=绿色、data2=蓝色、data3=黑色、data4=红色
我的数据
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_点
中的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()
或者,如果我们删除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\u id谢谢,但当在我的整个数据集上尝试时,它不起作用,我确切地知道我的值是否下降了,我尝试使用这些数据,即使我尝试使用每个类别的单个值,但给出了不同的颜色matching@Luckysardar我已更改为ggplot
,请参见编辑。@Luckysardar,我已经尝试使用我以前的代码将颜色与数据匹配,并且成功了,但是像这样,ggplot
将自动完成。我已经尝试过ggplot,它显示的正是我想要的,问题是我想要上一个图,请给我显示该图和代码的外观,我的情况是它看起来不一样
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)