How to combine two plots?

53 views Asked by At

Is it possible to combine two plots and make it one plot?

These are the plots I am trying to combine. I have tried the "combined_plot" command, but I get the same result - two separate plots.

[enter image description here](https://i.stack.imgur.com/rNHuA.png)

Here are my codes:

Plot 1:

plot1 <- ggplot() + 
   geom_bar(data = complete_august_data, aes(x = DATE, y = Count, fill = MANUAL.ID.), 
        stat = "identity", width = 0.7, position = "dodge") +
   geom_bar(data = complete_july_data, aes(x = DATE, y = Count, fill = MANUAL.ID.), 
        stat = "identity", width = 0.7, position = "dodge") +
   labs(title = "Kumulative kurver for juli og august ved Borupgård 2018",
        x = "Dato",
        y = "Kumulative observationer",
        fill = "Art") +
   scale_x_date(date_labels = "%Y-%m-%d") +
   theme_minimal()

Plot 2:

plot2 <- ggplot(result_df, aes(x = Dato, y = Belysning)) +
  geom_line(color = "gold2") +  # Tilføj en linje
  geom_point(color = "goldenrod", size = 2) +  # Tilføj punkter
  labs(x = "Dato", y = "Belysning", title = "Graf over belysning 25 juli - 29 august 2018") +
  scale_x_date(limits = as.Date(c("2018-07-26", "2018-08-29")), 
           date_breaks = "5 day", 
           date_labels = "%Y-%m-%d")+
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  theme_bw()
1

There are 1 answers

0
ZKA On

If you want a single plot with two y-axes, it's possible with ggplo2 but requires a bit of fiddling.

  1. You can add a second y-axis with a magnitude scaled relative to the first one using
+ scale_y_continuous(
    sec.axis = sec_axis(~./6000, name="Belysning")
  )

Here, I'm taking the first axis to go up to 6000. I would recommend manually setting the first y-axis-limit so the two are 100% congruent.

  1. Now you add the points/lines for "Belysning" to the plot.

Note, they're plotted relative to y-axis 1, so you need to rescale them.

+ geom_line( aes(y=Belysning * 6000), color="gold2")
  1. Combined, it should look something like:
plot_data <- cbind(complete_july_data,complete_august_data)
plot_data <- full_join(result_df, plot_data, join_by(Dato == DATE))

y_max <- 6000

plot <- ggplot(plot_data, aes(x=Dato)) +
   geom_bar(aes(y = Count, fill = MANUAL.ID.), 
        stat = "identity", width = 0.7, position = "dodge") +
   geom_line(aes(y = Belysning*y_max), color = "gold2") +
   geom_point(aes(y = Belysning*y_max), color = "goldenrod", size = 2) +
   labs(title = "Kumulative kurver for juli og august ved Borupgård 2018",
        x = "Dato",
        fill = "Art") +
   scale_y_continuous(
       limits = c(0,y_max),
       name = "Kumulative observationer",
       sec.axis = sec_axis(~./y_max, name="Belysning")
   ) + 
   scale_x_date(limits = as.Date(c("2018-07-26", "2018-08-29")), 
       date_breaks = "5 day", 
       date_labels = "%Y-%m-%d") +
   theme_bw() +
   theme(
       axis.text.x = element_text(angle = 45, hjust = 1),
       axis.title.y.right = element_text(color = "gold2")
   )

I tried simplifying by merging all three datasets into one. If that causes issues just add them in singularly like you were doing. But I created a sample dataset to look like yours and it gives me this:

enter image description here