ulima2_
ulima2_

Reputation: 1336

Fill point markers in ggplot2 figure depending on variable

I'm trying to visualize the following data for two countries:

library(tidyverse)

df <- tribble(
  ~year, ~country, ~value, ~choice,
  2001, "France", 55, TRUE,
  2002, "France", 53, TRUE,
  2003, "France", 31, FALSE,
  2004, "France", 10, FALSE,
  2005, "France", 30, FALSE,
  2006, "France", 37, FALSE,
  2007, "France", 54, FALSE,
  2008, "France", 58, FALSE,
  2009, "France", 50, TRUE,
  2010, "France", 40, TRUE,
  2011, "France", 49, TRUE,
  2001, "USA", 55, TRUE,
  2002, "USA", 53, TRUE,
  2003, "USA", 64, FALSE,
  2004, "USA", 40, FALSE,
  2005, "USA", 30, FALSE,
  2006, "USA", 39, FALSE,
  2007, "USA", 55, FALSE,
  2008, "USA", 53, TRUE,
  2009, "USA", 71, TRUE,
  2010, "USA", 44, TRUE,
  2011, "USA", 40, TRUE
)

I want to fill out markers for an observation, if column choice is TRUE. (In my real-life dataset this will correspond to values being significant.)

But somehow the fill command doesn't respond:

ggplot(df, aes(year, value, color = country)) +
  geom_line() +
  geom_point(aes(shape = country, fill = choice), size = 4) +
  theme_minimal() +
  scale_shape_manual(values = c(1, 2))

Which produces:

enter image description here

Upvotes: 1

Views: 1879

Answers (4)

rnso
rnso

Reputation: 24575

Using linetype may be more useful to convey message clearly:

ggplot(ddf, aes(year, value, color=country, linetype=choice))+ 
    geom_point()+ 
    geom_line()+ 
    theme_bw()

enter image description here

Upvotes: 1

jazzurro
jazzurro

Reputation: 23574

Following your way, I would do the following. I chose the plotting symbols that I can manipulate fill (i.e., 21 and 24). I assigned blue to TRUE and white to FALSE.

ggplot(df, aes(x = year, y = value)) +
geom_line(aes(color = country)) +
geom_point(aes(shape = country, fill = choice), size = 4, alpha = 0.4) +
theme_minimal() +
scale_shape_manual(values = c(21, 24)) +
scale_fill_manual(values = c("blue", "white"))

enter image description here

Given the OP's comment, here is another idea.

ggplot(df, aes(x = year, y = value)) +
geom_line(aes(color = country)) +
geom_point(aes(shape = interaction(country, choice), fill = interaction(country,choice)), size = 4, alpha = 0.4) +
theme_minimal() +
scale_shape_manual(values = c(21, 24, 21, 24)) +
scale_fill_manual(values = c("#F8766D", "#00BFC4", "white", "white"))

enter image description here

Upvotes: 2

ulima2_
ulima2_

Reputation: 1336

All great answers, but building on @PoGibas and @jazzurro's answers I came up with this which is closest to what I initially wanted:

df <- df %>% 
  mutate(types = paste(country, choice))

ggplot(df, aes(x = year, y = value, color = country)) +
  geom_line() +
  geom_point(aes(shape = types), size = 4, alpha = 0.6) +
  theme_minimal() +
  scale_shape_manual(values = c(16, 1, 17, 2))

enter image description here

The trick I used here is to create new variables combining country and choice.

Upvotes: 0

pogibas
pogibas

Reputation: 28359

Try different shapes.

# Using OP's data    
wantedShapes <- c(21, 24)
library(ggplot2)
ggplot(df, aes(year, value, color = country)) +
  geom_line() +
  geom_point(aes(shape = country, fill = choice), size = 4) +
  theme_minimal() +
  scale_shape_manual(values = wantedShapes)

enter image description here

You can find more on shapes here. The ones you tried to use don't have fill status.

However, I would use country only for color and shape for choice and fill (+ minimal visual tweaks):

ggplot(df, aes(year, value)) +
    geom_line(size = 1, aes(color = country)) +
    geom_point(aes(shape = choice, fill = choice), 
               size = 4, alpha = 0.6) +
    scale_shape_manual(values = wantedShapes) +
    scale_fill_brewer(palette = "Set1") +
    scale_color_brewer(palette = "Accent") +
    theme_minimal()

enter image description here

Upvotes: 1

Related Questions