1

My scatterplot I'm not the best with R, but I'm trying to introduce colour into this plot.

SecondPlot <- ggplot(sur11, aes(x=standec, y=compnoanti)) +
geom_point(col ="black", size = 0.5) +
geom_text(label=sur11$concatena, hjust = 0, nudge_x = 0.3, size = 2.5)
SecondPlot
SecondPlot + geom_abline(intercept = 50, slope = 0, size = 0.2)

How can I:

1) Colour all Y values beyond 75 points red?

2) Introduce scaled colouring so that I use an existing R brewer scale, e.g "Blues", to colour the Y value depending on their value?

I have tried assigning each y value to a point of 1 to 10 and then used the argument size = factor(Z) but this didn't work.

Thank you.

Henry Cann
  • 101
  • 1
  • 10

1 Answers1

1

Try this:

1) Colour all Y values beyond 75 points red

SecondPlot <- ggplot(sur11, aes(x=standec, y=compnoanti)) +
  geom_point(col = ifelse(sur11$compnoanti > 75, "red", "black"), size = 0.5) +
  geom_text(label=sur11$concatena, hjust = 0, nudge_x = 0.3, size = 2.5)
SecondPlot

2) Introduce scaled colouring so that I use an existing R brewer scale, e.g "Blues", to colour the Y value depending on their value?

#Interpolate Brewer palette
library(RColorBrewer)
colourCount = length(unique(sur11$compnoanti))
getPalette = colorRampPalette(brewer.pal(9, "Blues"))


SecondPlot <- ggplot(mtcars, aes(x=standec, y=compnoanti)) +
  geom_point(aes(col = compnoanti), size = 0.5) +
  geom_text(label=sur11$concatena, hjust = 0, nudge_x = 0.3, size = 2.5) +
  scale_colour_gradientn(colours=getPalette(colourCount))
SecondPlot

Since you didn't provide a reproducible example, I tested it using a generic dataset. This is the generic version:

# Color points above a certain value red
SecondPlot <- ggplot(mtcars, aes(x=mpg, y=disp)) +
  geom_point(col = ifelse(mtcars$disp > 120, "red", "black"), size = 0.5) +
  geom_text(label=rownames(mtcars), hjust = 0, nudge_x = 0.3, size = 2.5)
SecondPlot


#Interpolate Brewer palette
library(RColorBrewer)
colourCount = length(unique(mtcars$disp))
getPalette = colorRampPalette(brewer.pal(9, "Blues"))


SecondPlot <- ggplot(mtcars, aes(x=mpg, y=disp)) +
  geom_point(aes(col = disp), size = 0.5) +
  geom_text(label=rownames(mtcars), hjust = 0, nudge_x = 0.3, size = 2.5) +
  scale_colour_gradientn(colours=getPalette(colourCount))
SecondPlot
HAVB
  • 1,858
  • 1
  • 22
  • 37
  • This is amazing thank you very much. Just one question - if I wanted to colour the labels as well as the points how could I do that? Thank you @HAVB – Henry Cann Mar 31 '17 at 15:16
  • @HenryCann add `aes(colour = whateverVariable)` to the `geom_text` line. Check here for examples : http://docs.ggplot2.org/current/geom_text.html – HAVB Mar 31 '17 at 15:28
  • Thanks, I've been able to add colour to the label but not as a scale? Can I add the scale_colour_gradient argument to geom_text as an argument? Cheers @HAVB – Henry Cann Mar 31 '17 at 17:18
  • @HenryCann please provide a minimal reproducible example as a new question so other people can benefit from it, and I'm sure it'll be answered quickly by the SO community. I'll give it a shot myself. Useful: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – HAVB Mar 31 '17 at 21:44