Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ggplot-如何组合来自不同data.frames的两个散点图,并使用不同的颜色集来标识每个数据帧_R_Ggplot2_Scatter Plot - Fatal编程技术网

ggplot-如何组合来自不同data.frames的两个散点图,并使用不同的颜色集来标识每个数据帧

ggplot-如何组合来自不同data.frames的两个散点图,并使用不同的颜色集来标识每个数据帧,r,ggplot2,scatter-plot,R,Ggplot2,Scatter Plot,我希望创建一个散点图(x,y),从数据帧和颜色按性别,使用不同的颜色来区分不同数据帧的点。 例如-在df1中使用红色和浅红色表示性别,在df2中使用蓝色和浅蓝色表示性别您可以尝试此方法,确保可以更改颜色: # df1 x = c(3, 5, 8, 2) y = c(5, 4, 7, 9) gender = c(1, 0 ,0, 1) df1 = data.frame(x, y, gender) # df2 x = c(1, 6, 4, 8, 3, 6) y = c(2, 7,

我希望创建一个散点图(x,y),从数据帧和颜色按性别,使用不同的颜色来区分不同数据帧的点。
例如-在df1中使用红色和浅红色表示性别,在df2中使用蓝色和浅蓝色表示性别

您可以尝试此方法,确保可以更改颜色:

# df1
x = c(3, 5, 8, 2)
y = c(5, 4, 7, 9)
gender = c(1, 0 ,0, 1)
df1 = data.frame(x, y, gender)
        
# df2
x = c(1, 6, 4, 8, 3, 6)
y = c(2, 7, 8, 0, 2, 1)
gender = c(1, 0, 1, 0, 0, 1)
df2 = data.frame(x, y, gender)
    
库(ggplot2)
图书馆(拼凑)
#地块1

谢谢!有没有办法用两个数据帧中的点创建一个散点图。我的意思是,有没有办法创建一个包含两个数据帧中的点的散点图。点数是由性别决定的。但是每个数据帧都有独特的性别颜色集(例如,蓝色、浅蓝色代表df1,红色、浅红色代表df2)@Siddhartha我已经更新了解决方案。我希望有帮助!谢谢!我想知道是否有一种方法可以更简洁地书写传奇。我有一张有问题的示例图片。@Siddhartha您可以查找
guides()
,以便自定义您的腿。我在问题中没有看到任何图片。欢迎来到Stackoverflow,请尝试通过演示如何组合数据帧以及如何绘制数据帧来完成您的问题。
library(ggplot2)
library(patchwork)
#Plot 1
G1 <- ggplot(df1,aes(x=x,y=y,color=factor(gender)))+geom_point()+
  scale_color_manual(values=c('red','deeppink'))
#Plot 1
G2 <- ggplot(df2,aes(x=x,y=y,color=factor(gender)))+geom_point()+
  scale_color_manual(values=c('blue','darkviolet'))
#Combine
G1+G2
#Code
df1$id <- 'df1'
df2$id <- 'df2'
df <- rbind(df1,df2)

ggplot(df,aes(x=x,y=y,color=factor(interaction(id,gender))))+geom_point()+
  scale_color_manual(values=c('red','deeppink','blue','darkviolet'))