Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot conflict between fill and scale_fill_discrete/plot legend

Tags:

plot

r

ggplot2

I'm tinkering with geom_point trying to plot the following code. I have converted cars$vs to a factor with discrete levels so that I can visualize both levels of that variable in different colors by assigning it to "fill" in the ggplot aes settings.

cars <- mtcars
cars$vs <- as.factor(cars$vs)

ggplot(cars,aes(x = mpg, y = disp, fill = vs)) +
  geom_point(size = 4) +
  scale_fill_discrete(name = "Test")

The result is this:

As you can see, the graph does not differentiate between both "fill" conditions via color. However, it preserves the legend label I have specified in scale_fill_discrete.

Alternatively, I can plot the following (same code, but instead of "fill", use "color")

cars <- mtcars
cars$vs <- as.factor(cars$vs)

ggplot(cars,aes(x = mpg, y = disp, color = vs)) +
  geom_point(size = 4) +
  scale_fill_discrete(name = "Test")

enter image description here

As you can see, using "color" instead of "fill" differentiates between the levels of the factor via color, but seems to override any changes I make to the legend title using scale_fill_discrete.

Am I using "fill" incorrectly? How can I plot different levels of a factor in different colors using this method and have control over the plot legend vis scale_fill_discrete?

like image 705
pablom Avatar asked Feb 08 '17 00:02

pablom


People also ask

How to change the scale of a ggplot plot?

If you want to make more radical changes you will override the default scales with alternatives: ggplot (mpg, aes (displ, hwy)) + geom_point (aes (colour = class)) + scale_x_sqrt () + scale_colour_brewer () Here scale_x_sqrt () changes the scale for the x axis scale, and scale_colour_brewer () does the same for the colour scale.

How to change Legend labels in ggplot2?

How to Change Legend Labels in ggplot2 (With Examples) You can use the following syntax to change the legend labels in ggplot2: p + scale_fill_discrete (labels=c ('label1', 'label2', 'label3',...)) The following example shows how to use this syntax in practice.

What is the connection between non-position scale and legend in ggplot?

But the connection between non-position scales and legend is more complex: one legend may need to draw symbols from multiple layers (“merging”), or one aesthetic may need multiple legends (“splitting”). Merging legends occurs quite frequently when using ggplot2.

What is the use of scale_fill_viridis_C () function in ggplot2?

For example, ggplot2 supplies two scale functions that bundle pre-specified palettes, scale_fill_viridis_c () and scale_fill_distiller (). The viridis scales 36 are designed to be perceptually uniform in both colour and when reduced to black and white, and to be perceptible to people with various forms of colour blindness.


2 Answers

Since you are using color as mapping, you can use scale_color_* to change the corresponding attributes instead of scale_fill_*:

ggplot(cars,aes(x = mpg, y = disp, color = vs)) +
      geom_point(size = 4) +
      scale_color_discrete(name = "Test") 

enter image description here

like image 193
Psidom Avatar answered Nov 15 '22 09:11

Psidom


To use a fill with geom_point you should use a fill-able shape:

ggplot(cars,aes(x = mpg, y = disp, fill = vs)) +
  geom_point(size = 4, shape = 21) +
  scale_fill_discrete(name = "Test")

See ?pch, which shows that shapes 21 to 25 can be colored and filled with different colors.ggplot will not use the fill unless the shape is one that is fill-able. This behavior has changed a bit in different versions, as seen in the NEWS file.

There's no reason to use fill with geom_point unless you want the outline and fill colors of the points to be different, so the other answer recommending color is probably what you want.

like image 33
Gregor Thomas Avatar answered Nov 15 '22 09:11

Gregor Thomas