Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between the color and fill argument in ggplot2?

Tags:

r

ggplot2

ggmap(location) +
geom_density_2d(aes(long, lat), df) +
geom_point(aes(long, lat,**color = special**),alpha = 0.5,data = df) 

I can not see what is different when I change color to the fill, like:

ggmap(location) +
geom_density_2d(aes(long, lat), df) +
geom_point(aes(long, lat,**fill = special**),alpha = 0.5,data = df) 

what is the main difference between those two arguments?

like image 531
nevh Avatar asked May 28 '18 00:05

nevh


2 Answers

Generally, fill defines the colour with which a geom is filled, whereas colour defines the colour with which a geom is outlined (the shape's "stroke", to use Photoshop language).

Points generally only have a colour and no fill, because, y'know—they're just points. However, point shapes 21–25 that include both a colour and a fill. For example:

library(tidyverse)
df = data_frame(x = 1:5, y = x^2)
ggplot(df) +
  geom_point(
    aes(x, y, fill = x),
    shape = 21, size = 4, colour = 'red')

A ggplot with fill mapped to an aesthetic and colour fixed.

Here's an example with ggmap, where both fill and colour are set (but not mapped to aesthetics):

library("ggmap")

us = c(left = -125, bottom = 25.75, right = -67, top = 49)
map = get_stamenmap(us, zoom = 5, maptype = "toner-lite")
df2 = data_frame(
  x = c(-120, -110, -100, -90, -80),
  y = c(30, 35, 40, 45, 40))

ggmap(map) +
  geom_point(
    aes(x, y), data = df2,
    shape = 21, fill = 'blue', colour = 'red', size = 4)

A ggmap with fixed colour and stroke.

But unless you're using those special shapes, if you use a point, give it a colour, not a fill (because most points don't have one).

like image 103
jimjamslam Avatar answered Sep 29 '22 06:09

jimjamslam


This can't be answered much better than done so here.

The only reason I haven't marked this as duplicate as you're asking a slightly different question. They were experiencing what they thought was a bug, you are experiencing what you think is a no change.

In summary, there are several shapes you can choose for geom_point and only some of them have a fill and colour argument.

In general fill changes the colour within shapes, and colour changes the outline.

like image 38
LachlanO Avatar answered Sep 29 '22 06:09

LachlanO