ixodid
ixodid

Reputation: 2380

Can ggplot's faceting be used here?

Welcome to Tidyville.

Below is a small df showing the populations of cities in Tidyville. Some cities belong to the A state and some the B state.

I wish to highlight the cities that decreased in population in red. Mission accomplished so far.

But there are many states in Tidyville. Is there a way to use ggplot's faceting faceting to show a plot for each state. I'm uncertain because I'm new and I do a little calculation outside the ggplot call to identify the cities that decreased in population.

library(ggplot2)
library(tibble)

t1 <- tibble (
  y2001 = c(3, 4, 5, 6, 7, 8, 9, 10),
  y2016 = c(6, 3, 9, 2, 8, 2, 11, 15),
  type = c("A", "A", "B", "B", "A", "A", "B", "B")
)

years <-  15
y2001 <- t1$y2001
y2016 <- t1$y2016

# Places where 2016 pop'n < 2001 pop'n
yd <- y2016 < y2001

decrease <- tibble (
  y2001 = t1$y2001[yd],
  y2016 = t1$y2016[yd]
)

# Places where 2016 pop'n >= 2001 pop'n
yi <- !yd

increase <- tibble (
  y2001 = t1$y2001[yi],
  y2016 = t1$y2016[yi]
)

ggplot() + 
  # Decreasing
  geom_segment(data = decrease, aes(x = 0, xend = years, y = y2001, yend = y2016), 
             color = "red") +

  # Increasing or equal
  geom_segment(data = increase, aes(x = 0, xend = years, y = y2001, yend = y2016), 
             color = "black") 

Upvotes: 1

Views: 85

Answers (4)

iod
iod

Reputation: 7592

require(dplyr)
t1<-mutate(t1,decrease=y2016<y2001)
ggplot(t1)+facet_wrap(~type)+geom_segment(aes(x = 0, xend = years, y = y2001, yend = y2016, colour=decrease))

Upvotes: 0

MrFlick
MrFlick

Reputation: 206167

I think this would be much easier if you just put your data in a tidy format like ggplot2 expects. Here's a possible solution using tidyverse functions

library(tidyverse)
t1 %>% 
  rowid_to_column("city") %>% 
  mutate(change=if_else(y2016 < y2001, "decrease", "increase")) %>% 
  gather(year, pop, y2001:y2016) %>%
  ggplot() + 
    geom_line(aes(year, pop, color=change, group=city)) +
    facet_wrap(~type) + 
    scale_color_manual(values=c("red","black"))

This results in

enter image description here

Upvotes: 2

cparmstrong
cparmstrong

Reputation: 819

Your intermediary steps are unnecessary and lose some of your data. We'll keep what you created first:

t1 <- tibble (
  y2001 = c(3, 4, 5, 6, 7, 8, 9, 10),
  y2016 = c(6, 3, 9, 2, 8, 2, 11, 15),
  type = c("A", "A", "B", "B", "A", "A", "B", "B")
)
years <- 15

But instead of doing all the separating and subsetting, we'll just create a dummy variable for whether or not y2016 > y2001.

t1$incr <- as.factor(ifelse(t1$y2016 >= t1$y2001, 1, 0))

Then we can extract the data argument to the ggplot() call to make it more efficient. We'll only use one geom_segment() argument and set the color() argument to be that dummy variable we created before. We then need to pass a vector of colors to scale_fill_manual()'s value argument. Finally, add the facet_grid() argument. If you're only faceting on one variable, you put a period on the opposite side of the tilde. Period first mean's they'll be paneled side-by-side, period last means they'll be stacked on top of each toher

ggplot(data = t1) +
  geom_segment(aes(x = 0, xend = years, y = y2001, yend = y2016, color=incr)) +
  scale_fill_manual(values=c("black", "red")) +
  facet_grid(type~.)

Upvotes: 1

Rui Barradas
Rui Barradas

Reputation: 76402

I believe you don't need to create two new datasets, you can add a column to t1.

t2 <- t1
t2$decr <- factor(yd + 0L, labels = c("increase", "decrease"))

I have left the original t1 intact and altered a copy, t2.
Now in order to apply ggplot facets, maybe this is what you are looking for.

ggplot() + 
  geom_segment(data = t2, aes(x = 0, xend = years, y = y2001, yend = y2016), color = "red") +
  facet_wrap(~ decr)

If you want to change the colors, use the new column decr as an value tocolor. Note that this argument changes its position, it is now aes(..., color = decr).

ggplot() + 
  geom_segment(data = t2, aes(x = 0, xend = years, y = y2001, yend = y2016, color = decr)) +
  facet_wrap(~ decr)

Upvotes: 1

Related Questions