Home > Software engineering >  How to graph two different columns on one ggplot?
How to graph two different columns on one ggplot?

Time:05-10

I am trying to plot one column by Date (different color points for each animal category) and on the same graph, plot a second column by Date as well. The second column has entries for the days but only for certain categories, Large Dog. There is no adoption_with_discount for small or medium dogs (please see the reproducible example data set, example_data). When I plot them separately they visualize fine but not when plotted together. I thought I would just overlay a separate geom but that is not working.

I want to combine the two plots into one. My goal is for the points plot to have the line graph on top of it. I am trying to visualize the adoption as points colored by animal and put a line on the same graph of adoption_with_discount.

Thank you for your help!


# Make example  -----------------------------------------------------------
# Here is an example data set
# You can see in the `adoption_with_discount` the values I want to add as a line. 

library(lubridate)
library(tidyverse)

example_days <- data.frame(Date = c(seq.Date(from = as.Date('2022-03-01'), to = as.Date('2022-04-30'), by = 'days')))


example_small <-
  example_days %>%
  mutate(animal = "Small Dog")


a <-sample(100:150, nrow(example_small), rep = TRUE)

example_small <-
  example_small %>%
  mutate(adoption = a,
         adoption_with_discount = NA)

example_med <-
  example_days %>%
  mutate(animal = "Medium Dog")

b <-sample(150:180, nrow(example_med), rep = TRUE)

example_med <-
  example_med %>%
  mutate(adoption = b,
         adoption_with_discount = NA)


example_large <-
  example_days %>%
  mutate(animal = "Large Dog")


c <-sample(150:200, nrow(example_large), rep = TRUE)


example_large <-
  example_large %>%
  mutate(adoption = c)

example_large <-
  example_large %>%
  mutate(adoption_with_discount = adoption - 15)


example_data <- rbind(example_small, example_med, example_large)



# Plot --------------------------------------------------------------------

ggplot(data = example_data)  
  geom_point(mapping = aes(x = Date, 
             y = adoption,
             color = animal))  
  ggtitle("Dog Adoption by Size")  
  labs(x = "Date", y = "Adoption Fee")  
  scale_y_continuous(labels = scales::dollar)  
  theme(axis.text.x = element_text(angle = 45))

# Plot with Fee -----------------------------------------------------------
# This is where the problem is occurring
# When I want to add a line that plots the adoption with discount by day
# on top of the points, it does not populate. 

ggplot(data = example_data)  
  geom_point(mapping = aes(x = Date, 
                           y = adoption,
                           color = animal))  
  geom_line(mapping = aes(x = Date, 
                          y = adoption_with_discount), 
            color = "black")  
  ggtitle("Dog Adoption by Size with Discount Included")  
  labs(x = "Date", y = "Adoption Fee")  
  scale_y_continuous(labels = scales::dollar)  
  theme(axis.text.x = element_text(angle = 45))



# See if just Discount will Plot  -----------------------------------------
#This plots separately 
ggplot(data = example_large)  
  geom_line(mapping = aes(x = Date, 
                          y = adoption_with_discount), 
            color = "black")  
  ggtitle("Discount")  
  labs(x = "Date", y = "Adoption Fee")  
  scale_y_continuous(labels = scales::dollar)  
  theme(axis.text.x = element_text(angle = 45))


CodePudding user response:

Based on discussion enter image description here

CodePudding user response:

It looks like it is the NA that are included in the geom_line portion that is creating the issue so you can filter those out before plotting the line:

  geom_point(mapping = aes(x = Date, 
                           y = adoption,
                           color = animal))  
  geom_line(data=example_data %>% filter(!is.na(adoption_with_discount)),
            mapping = aes(x = Date, 
                          y = adoption_with_discount), 
            color = "black")  
  ggtitle("Dog Adoption by Size with Discount Included")  
  labs(x = "Date", y = "Adoption Fee")  
  scale_y_continuous(labels = scales::dollar)  
  theme(axis.text.x = element_text(angle = 45))

CodePudding user response:

While subsetting is an option to fix the issue, the reason why no line is plotted is simply the missing grouping, i.e. in geom_line you are trying to plot observations for all three dog types as one group or line. However, because of the NAs no line will show up. An easy option to solve that would be to explicitly map animal on the group aes. Additionally I added na.rm=TRUE to silent the warning about removed NAs. Finally I right aligned your axis labels by adding hjust=1:

library(ggplot2)

ggplot(data = example_data)  
  geom_point(mapping = aes(
    x = Date,
    y = adoption,
    color = animal
  ))  
  geom_line(
    mapping = aes(
      x = Date,
      y = adoption_with_discount,
      group = animal
    ),
    color = "black",
    na.rm = TRUE
  )  
  ggtitle("Dog Adoption by Size with Discount Included")  
  labs(x = "Date", y = "Adoption Fee")  
  scale_y_continuous(labels = scales::dollar)  
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

enter image description here

  • Related