1

So far, I have constructed a plot like this, but rather than 3 groupings there are quite a bit more. For now, this is just for illustration. I have made something like this:

#### Load Libraries ####
library(tidyverse)
library(ggpubr)
library(magick)

#### Create Main Plot ####
main <- iris %>% 
  ggplot(aes(x=Petal.Width,
             y=Petal.Length,
             color=Species))+
  geom_point()+
  geom_smooth(method = "lm")+
  theme(legend.position = "bottom")+
  labs(x="Frink Width",
       y="Frink Height",
       color="Frink Type")+
  scale_color_manual(labels = c("Frink1","Frink2","Frink3"),
                     values = c("lightblue","steelblue","navyblue"))
main

#### Import Image ####
imp <- frink <- image_read("https://jeroen.github.io/images/frink.png")
imp

#### Create Images as Plots ####
p1 <- image_ggplot(imp)+
  labs(title = "Frink 1")
p2 <- image_ggplot(imp)+
  labs(title = "Frink 2")
p3 <- image_ggplot(imp)+
  labs(title = "Frink 3")

#### Arrange ####
ps <- ggarrange(p1,p2,p3,
                ncol = 1)
ps

##### Smash Together ####
ggarrange(main,ps)

Which gives me a regular scatter on the left, and three images on the right:

enter image description here

The main thing I want to try to accomplish is to somehow move the color labels ("Frink1" and so on) to the right underneath each picture. How do I accomplish this? Keep in mind that for my specific purpose, the entire right region is filled with pictures, so this will have to be done for more than just these three in my case.

Shawn Hemelstrand
  • 2,676
  • 4
  • 17
  • 30
  • 1
    cowplot has a get_legend function, maybe useful, but then you need to split it into 3 images, maybe using gridExtra - https://stackoverflow.com/a/12041779/680068 – zx8754 Mar 23 '23 at 11:50
  • You could use `labs(caption = "Frink 1")` instead of `title`. You might need to add `theme(plot.caption = element_text(..your settings..))` to each of the image plots to get the optimal appearance / position, but the text will at least be under each image. – Allan Cameron Mar 23 '23 at 11:58
  • Well I'm not looking for the names so much as the actual line types (for example the light blue Frink line should appear below Frink 1 on the right). I'm less concerned with the actual text. – Shawn Hemelstrand Mar 23 '23 at 11:59

1 Answers1

2

Perhaps the easiest way to do this is create a legend with each image plot:

p1 <- image_ggplot(imp) + 
  geom_line(data = data.frame(x = 1:2, y = 1:2), aes(x, y, color = 'Frink1')) +
  scale_color_manual(NULL, values = "lightblue") +
  theme(legend.position = 'bottom')

p2 <- image_ggplot(imp)+
  geom_line(data = data.frame(x = 1:2, y = 1:2), aes(x, y, color = 'Frink2')) +
  scale_color_manual(NULL, values = "steelblue") +
  theme(legend.position = 'bottom')

p3 <- image_ggplot(imp) +
  geom_line(data = data.frame(x = 1:2, y = 1:2), aes(x, y, color = 'Frink3')) +
  scale_color_manual(NULL, values = "navyblue") +
  theme(legend.position = 'bottom')

ps <- ggarrange(p1,p2,p3,
                ncol = 1)
ps

ggarrange(main,ps)

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87