1

I have a small set of data where I want to give each point a different colour (Border and Fill), which I have specified within the data as extra columns. I've tried to classify these as colours rather than text using

colour = data[,5] 

and

colour <- as.character(data[,5])

but neither seems to work. R keeps just assigning random colours, ie. all the reds are the same colour, but not red! My plot looks like:

qplot(x, y, data, pch=21, col=Border, bg=Fill)

Not sure what else to do.

Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
Baresi6
  • 11
  • 2
  • ggplot likes to work with column names, not indices. So whatever the name is of your 5th column, say `"color_column"`, do `color = color_column`. – Gregor Thomas Oct 14 '15 at 21:36
  • Also, ggplot is different from base graphics, so base graphics options like `col` and `bg` won't work. The ggplot argument for the fill color is (nicely intuitive) `fill`, so you can do `fill = fill_column`. In that case, you'll also want to pick a shape that takes a fill, try `shape = 22`. – Gregor Thomas Oct 14 '15 at 21:37
  • Welcome to SO. For future reference, please read [this](http://stackoverflow.com/help/mcve) and [this](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). You are much more likely to get help if your questions conform to these guidelines. – jlhoward Oct 14 '15 at 21:46
  • @Gregor So my pch, col, fill commands are now working nicely but I still can't get it to register the correct colours. I've tried `colour = data["Fill"]`, where "Fill" is the name of the column but that hasn't worked. – Baresi6 Oct 15 '15 at 21:40
  • Just do `colour = Fill`, where `Fill` is the unquoted name of the column. You're already passing your data in, you don't need to re-specify it. – Gregor Thomas Oct 15 '15 at 21:56
  • If you share a small, reproducible data set, something like `dput(droplevels(head(data, 10)))`, I could actually show you code. – Gregor Thomas Oct 15 '15 at 21:58

1 Answers1

0

Without seeing sample data from you, let's use the diamonds data set built-in to ggplot2.

# really, let's use a subset
di = head(diamonds, 500)

I'd strongly recommend using ggplot rather than qplot:

ggplot(di, aes(x = carat, y = price, color = cut, fill = clarity)) +   
    geom_point(shape = 21)

Inside aes() you use unquoted column names. For something that is a constant, it goes outside the aes() aesthetic mappings, in this case shape = 21. (You can also use pch = 21 if you prefer the base graphics style name).

qplot is intended to be a shortcut, and it should work something like this:

qplot(data = di, x = carat, y = price, col = cut, fill = clarity, pch = 21)

but in this case it can't tell what's in your data (all the unquoted column names) and what's not (21), and since 21 is a numeric it assumes there will be a continuous scale for shapes, which isn't allowed. So there's an error.

A simpler plot would do fine with qplot

qplot(data = di, x = carat, y = price, col = cut)

but for this more complicated case, you're better off with ggplot as above.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • OK, so I have a table of data like this: `Team ShotsPG Possession PassSuccess Fill Border` `1 Manchester City 20.6 56.9 85.7 blue white` `2 Arsenal 20.0 56.5 85.6 red white` So I want each team to have a uniquely coloured point. Using the code you have given, I still have the same problem - it is not reading my "Fill" column as a list of colours. – Baresi6 Oct 16 '15 at 13:57
  • Right, that's not how ggplot works. You should have a column that's a factor where you want a different fill for each level. It will pick fill colors by default and give you a nice legend. If you want to specify colors, use `scale_fill_manual`. – Gregor Thomas Oct 17 '15 at 04:54
  • If you update your question with reproducibly shared data, like with `dput(droplevels(head(data, 10)))` I can maybe show more explicitly. Most any introduction to ggplot will cover this. – Gregor Thomas Oct 17 '15 at 04:55
  • Or you could see similar questions on SO, like [Pick color of choice... using ggplot](http://stackoverflow.com/q/24230094/903061), or [ggplot2 change colours how](http://stackoverflow.com/q/16308901/903061), which is actually my answer from a couple years ago. – Gregor Thomas Oct 17 '15 at 04:58