0

I am trying to create a complex plot with two axis.

I have created a facetwrap with the series of data that I am wanting to show. (In this case I am looking at element concentration over time with a few series on each plot). so Element values / Time.

The facet wrap is included to show the variable of each experiment.

enter image description here

However, with each plot/experiment here I have a additional data series that I would like to add to these plots.

1 - is this possible?

My current script is:

  ggplot(DF %>% mutate(Time = as.POSIXct(Time)),
       aes(x=Time, y=Values, group=Element, colour=Element)) +
  geom_point(aes(shape=Element), 
               size=1.5, alpha=.7) +
  geom_line(linetype = "dashed") +
  labs(x="Time (hh:ss)", y="Element Concentration (ng/ml)") +
  theme_bw() +
  theme(axis.title.x=element_text(vjust=0, size=12),
        axis.title.y=element_text(vjust=2, size=12),
        axis.text.x=element_text(angle=10, hjust=1, vjust=1),
        strip.background = element_blank(),
        legend.position = 'bottom', legend.title=element_blank()) +
  facet_wrap(BothLabels~., scales = "free_y") +
  scale_x_datetime(date_labels = "%H:%M", date_breaks = "2 hour")

This doesn't account for the temperature (this is want I would like to add on a second axis)

Head:

  Time   Medium Size       Temperature Element Values
  <time> <fct>  <chr>            <dbl> <chr>    <dbl>
1 01:00  Cubed  "<63\xb5m"          15 Mg      38570.
2 02:00  Cubed  "<63\xb5m"          29 Mg       2957.
3 03:00  Cubed  "<63\xb5m"          29 Mg      17445.
4 04:00  Cubed  "<63\xb5m"          22 Mg       2561.
5 05:00  Cubed  "<63\xb5m"          19 Mg       2251.
6 06:00  Cubed  "<63\xb5m"          13 Mg       4697.

Is there a simple way to add a series here on another axis - or a better way of producing this code

  • Lots of questions on this, see e.g. https://stackoverflow.com/questions/26917689/how-to-use-facets-with-a-dual-y-axis-ggplot. In short, yes, but it's a bit cumbersome since this kind of dual axis is considered by many to be a bad idea (see e.g. Hadley's answer [here](https://stackoverflow.com/questions/3099219/ggplot-with-2-y-axes-on-each-side-and-different-scales?rq=1)) – Axeman Feb 11 '22 at 21:06

0 Answers0