0

I have plotted by data (from a .csv file) into a 3D scatter plot using scatter3D(). 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 -i.e. I want a 2D projection of my currently 3D scatter plot with the same colour scheme for all the points as before (based on the z- value).

How do I go about this?

Alternatively- is there a way I can plot a 2D graph, with the z value indicating the colour of the point plotted at (x,y)?

UseR10085
  • 7,120
  • 3
  • 24
  • 54
  • Provide a sample dataset and the code you have tried so far. Please visit [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – UseR10085 Nov 28 '20 at 08:11

1 Answers1

0

If your z variable is a factor (category):

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'))

enter image description here

If your z variable is a continuous variable:

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')

enter image description here

Eric Krantz
  • 1,854
  • 15
  • 25