Kristen Cyr
Kristen Cyr

Reputation: 726

how to fix, Geom_point working but geom_line not working when graphing with 2 y graphs on ggplot?

I'm trying to map 2 different sets of data on the same x-axis but with different y axis. This is what my dataframe (individual_dets) looks like

month      num_unique_tags     ave_temp    year     zone  

Jan             7                1.16       2016      2
Jan             7                1.16       2017      1
Feb             6                1.17       2018      1   
Feb             6                1.17       2018      2
Mar             1                2.0        2016      3
Jun             2                2.3        2016      4
Apr             1                2.0        2016      4
Apr             1                3.1

So the x axis is month, the left y-axis is num_unique_detections and the right y axis is ave_temp. When I plot with the following code.

ggplot(individual_dets, aes(x = month, y = num_unique_tags)) +
#Graph month * unique_dets
  geom_bar(aes(fill = zone), position = "dodge", stat = "identity") +
#Graph the temperature
  geom_point(mapping = aes(x = month, y = ave_temp), color = "black", data = individual_dets) +
#Make graph with 2 y axis
  scale_y_continuous(name = "Total Unique Detections", 
                     sec.axis = sec_axis(~ . -2/2, name = "Ave Temp")) +
  scale_fill_manual(values=c("#01579B", "#4FC3F7", "#ffa600", "#ff6361")) +
  ylim(0,8) +
  xlab("Month") +
  ylab("Total Unique Detections") +
#Facet wrap by year
  facet_wrap(~ year, scales = "free", nrow = 3, strip.position = "top") + 
  theme_bw() + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(),

        strip.background =  element_rect(fill = NA, colour = NA), 
        strip.text =        element_text(size = 25),

        text = element_text(family = "sans", size = 24))

I get this picture enter image description here

which is good except, I want the points to be a line... but when I use geom_line instead of geom_point using this code

ggplot(individual_dets, aes(x = month, y = num_unique_tags)) +
#graph the month * unique_dets
  geom_bar(aes(fill = zone), position = "dodge", stat = "identity") +
#Graph the temperature, but not showing up
  geom_line(mapping = aes(x = month, y = ave_temp), color = "black", data = individual_dets) +
#Make 2 different y axis
  scale_y_continuous(name = "Total Unique Detections", 
                     sec.axis = sec_axis(~ . -2/2, name = "Ave Temp")) +
#Hex codes to denote the zone
  scale_fill_manual(values=c("#01579B", "#4FC3F7", "#ffa600", "#ff6361")) +
  ylim(0,8) +
  xlab("Month") +
  ylab("Total Unique Detections") +
#Facet wrap by year
  facet_wrap(~ year, scales = "free", nrow = 3, strip.position = "top") + 
  theme_bw() + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(),

        strip.background =  element_rect(fill = NA, colour = NA), 
        strip.text =        element_text(size = 25),

        text = element_text(family = "sans", size = 24))

I get an image with no line, just the bars

enter image description here

I'm not sure what I'm doing wrong, does anyone know how to fix this?

Upvotes: 1

Views: 1475

Answers (1)

dc37
dc37

Reputation: 16178

You need to add group = 1 in the aes of your geom_line:

ggplot(df, aes(x = month, y = num_unique_tags))+
  geom_col(position = position_dodge(), aes(fill = as.factor(zone)))+
  geom_line(inherit.aes = FALSE, data = df,
             aes(x = month, y = ave_temp, group = 1), color = "black")+
  scale_x_discrete(limits = c("Jan","Feb","Mar","Apr","Jun"))+
  scale_y_continuous(limits = c(0,8), name = "Total Unique Detections", 
                     sec.axis = sec_axis(~ . -2/2, name = "Ave Temp")) +
  scale_fill_manual(name = "Zone",values=c("#01579B", "#4FC3F7", "#ffa600", "#ff6361"))+
  xlab("Month")+
  facet_wrap(~ year, scales = "free", nrow = 3, strip.position = "top") + 
  theme_bw() + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(),
        strip.background =  element_rect(fill = NA, colour = NA), 
        strip.text =        element_text(size = 25),
        text = element_text(family = "sans", size = 24))

enter image description here

Is it what you are trying to achieve ?

PS: I made minor edition in your code to modify few points (geom_col instead of geom_bar(sat = "identity", delete ylab and set the y-axis name by adding the argument name in scale_y_continuous and the use of scale_x_discrete to order your x-axis by months)

Example Data

structure(list(month = c("Jan", "Jan", "Feb", "Feb", "Mar", "Jun", 
"Apr"), num_unique_tags = c(7L, 7L, 6L, 6L, 1L, 2L, 1L), ave_temp = c(1.16, 
1.16, 1.17, 1.17, 2, 2.3, 2), year = c(2016L, 2017L, 2018L, 2018L, 
2016L, 2016L, 2016L), zone = c(2L, 1L, 1L, 2L, 3L, 4L, 4L)), row.names = c(NA, 
-7L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x5600fdd64350>)

Upvotes: 4

Related Questions