繁体   English   中英

R-我如何将 3D 散点 plot 展平为二维 plot?

[英]R- How do I flatten a 3D scatter plot to a 2D plot?

我已经使用scatter3D()将数据(来自 a.csv 文件)绘制到 3D scatter plot 中。 I want to turn this into a 2-D plot, where the colour of each of the plotted points is the same as it is in the 3D plot -ie I want a 2D projection of my currently 3D scatter plot with the same colour scheme for所有点和以前一样(基于 z 值)。

我该如何 go 关于这个?

或者 - 有没有办法可以 plot 二维图形,z 值指示绘制在 (x,y) 处的点的颜色?

如果您的z变量是一个因素(类别):

library(ggplot)
df <- data.frame(x = 1:18, y = runif(18), z = c('A', 'B', 'C'))    
ggplot(df, aes(x, y)) +
    geom_point(aes(color = z)) +
    # optional
    scale_color_manual(values = c('red', 'blue', 'green'))

在此处输入图像描述

如果您的z变量是连续变量:

library(ggplot2)
df <- data.frame(x = 1:18, y = runif(18), z = runif(18, min = 1, max = 100))
ggplot(df, aes(x, y)) +
    geom_point(aes(color = z)) +
    # optional
    scale_color_continuous(low = 'red', high = 'green')

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM