Ajrhjnd
Ajrhjnd

Reputation: 330

How to overlap two different plots in R

I need to overlap two different plots. They use the same scale already.

My code for each separated scatterplot look like this.

 ggscatter(chemicals, x = "columnB", y = "columnA", 
          color = "nombre",
          palette = "jco",
          ellipse = FALSE,
          ellipse.type = "convex",
          repel = TRUE,
          max.overlaps = 10,
          font.label = c(6, "plain", "red"))

  ggscatter(rivers, x = "V3", y = "V2",
          label = rivers$V1,
          palette = "jco",
          ellipse = FALSE,
          ellipse.type = "convex",
          repel = FALSE,
          max.overlaps = 10,
          font.label = c(6, "plain", "blue"))

The first data look like this...

chemicals <- structure(list(columnA = c(0.34526, -0.47491, 1.9717, -1.28922, 
                         -1.3365, -1.06089, -1.35741, -1.03362, 1.33577, 0.26619, -1.33583, 
                         0.56619, -0.84651, 0.52487, -0.44644, 0.33894, 1.33558, -1.36652, 
                         -1.41608, 0.08864, -0.98665, -0.13102, 0.96633, -0.33869, -1.45537, 
                         1.50434, -1.30283, -0.03662, -0.83985, -0.86605, 0.96659, -1.37216, 
                         1.05501, 0.34936, -0.56608, -0.84148, 1.16633, 1.15391, -1.10533, 
                         -0.04087, 1.36684, 0.39588, -0.4166, -0.7338, -1.33663, 1.24798, 
                         0.26939, 0.57514, 0.21976, -0.62348, -1.3341, 0.6696, 1.71274, 
                         0.0337, -1.33959, -0.33319, -0.21368, -0.25305, 0.56606, 0.56665
), columnB = c(0.46696, 0.15238, 0.28205, -1.01343, -0.45548, -0.58032, 
             -0.03174, -1.86618, 0.37332, 0.33668, 0.3668, 0.67415, -0.0393, 
             1.21716, 0.06624, 1.4333, 0.42663, 0.33143, 0.33529, -2.66816, 
             0.76601, 0.06666, 0.86633, 0.59532, -0.33115, -0.76641, 0.06633, 
             0.50038, -0.11718, 0.28718, -1.84348, -0.2598, -0.37834, 1.82102, 
             0.66669, 0.56604, -2.17667, -1.86617, 0.67087, -2.2598, -2.06249, 
             -0.25863, 1.26661, -1.76684, 0.06665, 0.80114, -1.33408, 0.23333, 
             0.21658, 0.39268, 0.50466, -0.09929, -0.09178, 1.07363, 1.15409, 
             -0.49409, 1.628, 0.26664, 0.62084, 0.50397)), row.names = c(1L, 
                                                                         2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
                                                                         13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 
                                                                         24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 
                                                                         35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 
                                                                         46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 
                                                                         57L, 58L, 59L, 60L), class = "data.frame")

The second data looks like this...

rivers <- structure(list(V1 = structure(c(7L, 5L, 6L, 1L, 3L, 4L, 8L, 2L
), .Label = c("riverA", "riverB", "riverC", "riverD", 
              "riverE", "riverF", "riverG", "riverH"), class = "factor"), 
V2 = structure(c(8L, 7L, 6L, 5L, 4L, 1L, 2L, 3L), .Label = c("-0.800", 
                                                             "0.021", "0.220", "0.590", "0.999", "0.333", "0.700", "0.850"
), class = "factor"), V3 = structure(c(1L, 3L, 4L, 2L, 7L, 
                                       6L, 8L, 5L), .Label = c("-0.028", "-0.011", "-0.078", "-0.4", 
                                                               "-0.952", "0.275", "0.630", "0.725"), class = "factor")), class = "data.frame", row.names = c(NA, 
                                                                                                                                                             -8L))

I need to put both of these scatter plots together in one plot.

Upvotes: 0

Views: 606

Answers (1)

r2evans
r2evans

Reputation: 161110

I don't have ggpubr, but here is a demonstration using ggplot2:

library(dplyr)
library(ggplot2)
rivers %>%
  mutate(source = "rivers", across(c(V3,V2), ~ as.numeric(as.character(.)))) %>%
  select(source, columnA = V3, columnB = V2) %>%
  bind_rows(mutate(chemicals, source = "chemicals")) %>%
  ggplot(aes(columnA, columnB)) +
  geom_point(aes(color = source))

sample ggplot2 scatterplot

I'm guessing this should be straight-forward to translate into ggpubr::ggscatter.

The premise of row-binding (via base rbind or dplyr::bind_rows or data.table::rbindlist) is that the number of rows matters not, it's the columns that matter. In the base case, there must be the same number of columns with the same names:

dat1 <- data.frame(a = 1, b = 2)
dat2 <- data.frame(a = 1:2, d = 3:4)
rbind(dat1, dat2)
# Error in match.names(clabs, names(xi)) : 
#   names do not match previous names

dat2b <- data.frame(a = 1:2, b = 3:4)
rbind(dat1, dat2b)
#   a b
# 1 1 2
# 2 1 3
# 3 2 4

Both dplyr::bind_rows and data.table::rbindlist provide wiggle room around this, either by default (former) or with options (latter):

dat2 <- data.frame(a = 1:2, d = 3:4)
dplyr::bind_rows(dat1, dat2)
#   a  b  d
# 1 1  2 NA
# 2 1 NA  3
# 3 2 NA  4
data.table::rbindlist(list(dat1, dat2), use.names = TRUE, fill = TRUE)
#        a     b     d
#    <num> <num> <int>
# 1:     1     2    NA
# 2:     1    NA     3
# 3:     2    NA     4

In this case, though, you want to normalize the names, so for one of them you need to change in either or both of them so that they can be aligned/row-bound properly.

FYI, you don't actually have to rename or rbind them to do things the brute-force way in ggplot2, but doing it this way has consequences and limits several other options so it is generally discouraged:

ggplot() +
  geom_point(aes(columnA, columnB), color = "red", data = chemicals) +
  geom_point(aes(as.numeric(as.character(V3)), as.numeric(as.character(V2))), color = "blue", data = rivers)

... but this doesn't help you adapt the process to ggscatter, so it is doubly not useful. I'll keep it, but don't go down this last path.

Upvotes: 2

Related Questions