1

I want to plot a scatter plot with four variables with one variable as a factor. This is the expected plot below

enter image description here

I'm not able to plot with this code

omgp <- ggplot(Q, aes(x = Sal, y = Tmp, colour = as.factor(Station), )) + 
  geom_point(size = 3 ) + 
  scale_color_gradient(low = "#AF7AC5", high = "#E74C3C") + 
  scale_x_continuous(name = 'Salinity') + 
  scale_y_continuous(name =  Temperature °C') +
  theme_bw() + 
  ggtitle('Scatter plot Calcite Saturation State and 
  Temperature')

Here is the error message I'm getting Can anyone help me with code to reproduce this scatter plot?

s__
  • 9,270
  • 3
  • 27
  • 45
  • Welcome to SO Adekunbi! To be able to help you, we need to know what data you're using. Please see [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Dion Groothof Jan 02 '22 at 21:45
  • It looks like you forgot to include the error message, and without any data, we can't run your code to find out what the error message is. I'd guess it could be related to the typo in `name = Temperature °C'` (missing quotation mark) – camille Jan 03 '22 at 00:51

1 Answers1

1

Welcome to SO! Your code is not reproducible, next time make it as it's possible to create your issues in other machines.

So here an answer with iris dataset, you can easily tweak it.

First of all there are some errors in your code, in aes() you got a , in scale_y_continuous() a ' not closed (or opened).

Try this:

library(ggplot2)
# shape is going to give the shapes as in your example
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Petal.Length, shape = Species)) +
  geom_point() +
   # here the colour you need, note that it could not be perfect
   # for reading data: rev() reverse the scale, to have red on
   # higher values
  scale_colour_gradientn(colours = rev(rainbow(5)))+
  scale_x_continuous(name = 'Salinity') + 
  scale_y_continuous(name =  'Temperature °C') +
  theme_bw() + 
  ggtitle('Scatter plot Calcite Saturation State and Temperature')

enter image description here

s__
  • 9,270
  • 3
  • 27
  • 45