关于r:scatterplot,其点基于ggplot 2中的颜色和因子大小

scatterplot with points based on color and size of factor in ggplot 2

我是ggplot2的新手,请多多包涵。 我想在ggplot2中绘制一个散点图,在其中可以为数据着色或根据第二个可变因子更改点的大小。 我可以使用plot()函数来实现这种颜色:

1
2
3
4
5
6
#simulate data
x1 <- rnorm(100)
y <- as.factor(runif(100)<=.70)
df <- data.frame(x1,y)
#plot
plot(df$x1, col = df$y,cex = 1, pch = 19)

这是我对ggplot2的尝试:

1
qplot(seq_along(df$x1), df$x1) + scale_colour_manual(breaks = df$y)


通过在colaes中指定colshape的颜色和形状,可以实现预期的输出。

1
2
library(ggplot2)
ggplot(df, aes(x=x1, y=seq(1,length(x1)),col=y, shape=y)) + geom_point()

输出:

enter image description here


您的问题有一个标题:基于ggplot 2中因数的颜色和形状的点的散点图

而问题中的文字说:...我可以在其中为数据着色或更改点的大小...

你在找什么? 颜色,形状或大小?

您可以在ggplot()中使用shapesizecolour自变量:

1
2
library(ggplot2)
ggplot(df, aes(x=seq(1,length(x1)), y=x1,colour=y, size=y)) + geom_point()

在此示例中,我只是设置大小(size=...)和颜色(color=...)应该取决于y变量,但是您可以根据需要更改它。 对于shape参数,只需使用shape=...

但是:建议不要将大小用于离散变量。

enter image description here