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))
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
I'm not sure what I'm doing wrong, does anyone know how to fix this?