1

see time series plotI have made a time series plot for total count data of 4 different species. As you can see the results with sharksucker have a much higher count than the other 3 species. To see the trends of the other 3 species they need to plotted separately (or on a smaller y axis). However, I have a figure limit in my masters paper. So, I was trying to create a dual axis plot or have the y axis split into two. Does anyone know of a way I could do this?

library(tidyverse) 
library(reshape2)
dat <- read_xlsx("ReefPA.xlsx")
dat1 <- dat
dat1$Date <- format(dat1$Date, "%Y/%m")
plot_dat <- dat1 %>%
  group_by(Date) %>%
  summarise(Sharksucker_Remora = sum(Sharksucker_Remora)) %>%
  melt("Date") %>%
  filter(Date > '2018-01-01') %>%
  arrange(Date)
names(plot_dat) <- c("Date", "Species", "Count")
ggplot(data = plot_dat) +
  geom_line(mapping = aes(x = Date, y = Count, group = Species, colour = Species)) +
  stat_smooth(method=lm, aes(x = Date, y = Count, group = Species, colour = Species)) + 
  scale_colour_manual(values=c(Golden_Trevally="goldenrod2", Red_Snapper="firebrick2", Sharksucker_Remora="darkolivegreen3", Juvenile_Remora="aquamarine2")) + 
  xlab("Date") + 
  ylab("Total Presence Per Month") + 
  theme(legend.title = element_blank()) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) 
r2evans
  • 141,215
  • 6
  • 77
  • 149
  • 1
    Does this answer your question? [Plot line and bar graph (with secondary axis for line graph) using ggplot](https://stackoverflow.com/questions/53922846/plot-line-and-bar-graph-with-secondary-axis-for-line-graph-using-ggplot) – Adam Sampson Aug 12 '20 at 20:49
  • 1
    @AdamSampson's suggested link is one dupe, or you can search SO for [`[r] ggplot2 sec_axis`](https://stackoverflow.com/search?q=%5Br%5D+ggplot2%20sec_axis), lots of examples of using `ggplot2::sec_axis`. Granted, not all of them are about "how to add a second axis", but the number of examples should give you plenty of sample code to absorb and adapt. – r2evans Aug 12 '20 at 20:54
  • 2
    Aimee, adding a second axis in `ggplot2` was a long and hard-fought battle with the core developers, and for a good reason: a second axis can and often is *confusing*, *misleading*, or both. In this case, I'd lean strongly towards "both". The values are in the same units, so adding a second axis just to bring one line closer to the other is not good. (IMHO, you need to actively avoid introducing known bias into plots and figures. Had I done that on my thesis, my advisor would have sent it back immediately.) – r2evans Aug 12 '20 at 21:03
  • 2
    I second @r2evans. If you really want to show that the trends have the same shape you might consider normalizing or standardizing your data instead! That informs the viewer that the data has been manipulated simply by labelling the new graph in units of standard deviations from the mean (or z-score). – Adam Sampson Aug 12 '20 at 21:34
  • Perhaps a log transformation? – Mark Neal Aug 13 '20 at 01:56

1 Answers1

1

The thing is, the problem you're trying to solve doesn't seem like a 2nd Y axis issue. The problem here is of relative scale of the species. You might want to think of something like standardizing the initial species presence to 100 and showing growth or decline from there.

Another option would be faceting by species.

greg dubrow
  • 623
  • 6
  • 9
  • 2
    or use`scale_y_log10()` ... if faceting you could either put each species in a separate facet, or just facet common (sharksucker) vs rare (other spp) – Ben Bolker Aug 12 '20 at 20:57