0

How do I create a scatter-plot in ggplot() with each points coloured manually? The necessary colours are given in my dataframe.

> head(df) x y col 1 0.72 2757 #2AAE89 2 0.72 2757 #2DFE83 3 0.72 2757 #40FE89 4 0.70 2757 #28FE97 5 0.86 2757 #007C7D 6 0.75 2757 #24FEA1 

The colour of the points must be exactly as given in the dataframe

4
  • 1
    Do not edit the question to invalidate existing answers. Commented Aug 11, 2019 at 13:07
  • There's nothing I can do about the duplicated post - consider checking the linked answers. You cannot change your question once it is answered - that's how the site works. It wastes the efforts of the answerers Commented Aug 11, 2019 at 14:46
  • @YvetteColomb, checked already, no relevant answers exist. Commented Aug 11, 2019 at 14:48
  • then you need to reword your question. There's a limit to what I can do to help you in the comments. Sorry. Commented Aug 11, 2019 at 14:56

1 Answer 1

3

Luckily there is a relatively easy solution by using scale_colour_identity(), see the following example:

library(ggplot2) z <- " x y z col 1 0.72 2757 86 #2AAE89 2 0.72 2757 86 #2DFE83 3 0.72 2757 86 #40FE89 4 0.70 2757 82 #28FE97 5 0.86 2757 26 #007C7D 6 0.75 2757 79 #24FEA1" df <- read.table(text = z, header = T) ggplot(df, aes(x, y, colour = col)) + geom_point() + scale_colour_identity() 

enter image description here

EDIT: I made a mistake in loading in the data, but the plotting syntax is still valid.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.