0

I would like to overlay the points from two different dataframes on the same graph but I can't seem to achieve it.

I know that I need to combine the 2 ggplot() but I don't know which method to use.

  ggplot(grid) + 
ggtitle("top+bot") +
aes(x, y) + 
geom_point(shape=15, color = "blue", size = 2, alpha = (grid$z/op)) +
scale_x_continuous(name= "length (m)", breaks=seq(0, max(fullgrid[,"x"]), step)) +
scale_y_continuous(name= "width (mm)", breaks=seq(0, max(fullgrid[,"y"]), 100)) +
expand_limits(x = 0, y = 0) +
coord_cartesian(expand = FALSE) +
theme(plot.title = element_text(size=10, hjust = 0.5), axis.title.x = element_text(size=10), axis.text.x= element_text(angle=30, hjust=1), axis.title.y = element_text(size=10), axis.text.y= element_text(angle=30, hjust=1))

  ggplot(def) + 
ggtitle("top+bot") +
        
geom_rect(data = def , aes(xmin = xbegc, xmax = xendc, ymin = ybegc, ymax =yendc ), 
                  alpha = 8) 
  • You can use `+`. `ggplot(grid) + ... {{all your grid code}} + geom_rect(data = df, aes...)` – Gregor Thomas Feb 16 '23 at 15:36
  • I'm not sure you do have to overlay two graphs, could you please provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) that has some data we could use to generate a solution? – DaveArmstrong Feb 16 '23 at 15:37
  • Note that each `geom` can be supplied its own separate `dataframe` like: `ggplot() + geom_point(data = df1, ...) + geom_point(data = df2, ...)` – I_O Feb 16 '23 at 15:39

1 Answers1

0

There are many things wrong in your code. I've boiled down your code to something more essential - to crystallise the problem: The use of a global aesthetic for all geom layers, although not all data frames have this aesthetic (in your case: mod).

I've also used a smaller version of your data and slightly changed your regex.

Smaller comments in the code - I recommend to read.

library(ggplot2)
freq <- structure(list(vlookup = c("Entrevista_final|1|Q3_nova|0|C1|1", "Entrevista_final|1|Q3_nova|0|C1|2", "Entrevista_final|1|Q3_nova|0|C3|1", "Entrevista_final|1|Q3_nova|0|C3|4", "Entrevista_final|1|Q3_nova|0|C3|2", "Entrevista_final|1|Q3_nova|0|C3|3", "Entrevista_final|1|Q3_nova|0|C4_1|2018", "Entrevista_final|1|Q3_nova|0|C4_1|2020", "Entrevista_final|1|Q3_nova|0|C4_1|1993", "Entrevista_final|1|Q3_nova|0|C4_1|2015", "Entrevista_final|1|Q3_nova|0|C4_1|2016", "Entrevista_final|1|Q3_nova|0|C4_1|1996", "Entrevista_final|1|Q3_nova|0|C4_1|99", "Entrevista_final|1|Q3_nova|0|C4_1|2017", "Entrevista_final|1|Q3_nova|0|C4_1|2004", "Entrevista_final|1|Q3_nova|0|C4_1|2019", "Entrevista_final|1|Q3_nova|0|C4_1|2002", "Entrevista_final|1|Q3_nova|0|C4_1|2021", "Entrevista_final|1|Q3_nova|0|C4_2|99", "Entrevista_final|1|Q3_nova|0|C4_2|2018"), cruza1 = c("Entrevista_final", "Entrevista_final", "Entrevista_final", "Entrevista_final", "Entrevista_final", "Entrevista_final", "Entrevista_final", "Entrevista_final", "Entrevista_final", "Entrevista_final", "Entrevista_final", "Entrevista_final", "Entrevista_final", "Entrevista_final", "Entrevista_final", "Entrevista_final", "Entrevista_final", "Entrevista_final", "Entrevista_final", "Entrevista_final"), mod1 = c("1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1"), cruza2 = c("Q3_nova", "Q3_nova", "Q3_nova", "Q3_nova", "Q3_nova", "Q3_nova", "Q3_nova", "Q3_nova", "Q3_nova", "Q3_nova", "Q3_nova", "Q3_nova", "Q3_nova", "Q3_nova", "Q3_nova", "Q3_nova", "Q3_nova", "Q3_nova", "Q3_nova", "Q3_nova"), mod2 = c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), var = c("C1", "C1", "C3", "C3", "C3", "C3", "C4_1", "C4_1", "C4_1", "C4_1", "C4_1", "C4_1", "C4_1", "C4_1", "C4_1", "C4_1", "C4_1", "C4_1", "C4_2", "C4_2"), mod = c("1", "2", "1", "4", "2", "3", "2018", "2020", "1993", "2015", "2016", "1996", "99", "2017", "2004", "2019", "2002", "2021", "99", "2018"), pop_extrapolada = c(22, 17, 32, 3, 3, 1, 4, 10, 1, 1, 2, 1, 2, 2, 1, 4, 1, 10, 2, 3), PERCENT = c(56.4102564102564, 43.5897435897436, 82.051282051282, 7.69230769230769, 7.69230769230769, 2.56410256410256, 10.2564102564103, 25.6410256410256, 2.56410256410256, 2.56410256410256, 5.12820512820513, 2.56410256410256, 5.12820512820513, 5.12820512820513, 2.56410256410256, 10.2564102564103, 2.56410256410256, 25.6410256410256, 15.3846153846154, 23.0769230769231), count = c(22, 17, 32, 3, 3, 1, 4, 10, 1, 1, 2, 1, 2, 2, 1, 4, 1, 10, 2, 3), BA = c(39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 13, 13), StdErr = c(0.0795074974876938, 0.0795074974876938, 0.0615313285518115, 0.0427252055542318, 0.0427252055542318, 0.0253433535020364, 0.0486447864199686, 0.0700118062133025, 0.0253433535020364, 0.0253433535020364, 0.0353661791821834, 0.0253433535020364, 0.0353661791821834, 0.0353661791821834, 0.0253433535020364, 0.0486447864199686, 0.0253433535020364, 0.0700118062133025, 0.100484337629983, 0.117340426124568), LowerCLMean = c(0.407774134748444, 0.279569006543315, 0.699529312635895, 0, 0, 0, 0.00691824151210088, 0.118752349327158, 0, 0, 0, 0, 0, 0, 0, 0.00691824151210088, 0, 0.118752349327158, 0, 0), UpperCLMean = c(0.720430993456685, 0.592225865251556, 0.941496328389746, 0.160929799423163, 0.160929799423163, 0.0754713784093646, 0.198209963616104, 0.394068163493355, 0.0754713784093646, 0.0754713784093646, 0.120819383213079, 0.0754713784093646, 0.120819383213079, 0.120819383213079, 0.0754713784093646, 0.198209963616104, 0.0754713784093646, 0.394068163493355, 0.352798149154327, 0.463095108198593), ME = c(15.6328429354121, 15.6328429354121, 12.0983507876925, 8.40067225000863, 8.40067225000863, 4.9830352768339, 9.56458610520017, 13.7657907083098, 4.9830352768339, 4.9830352768339, 6.95373319310279, 4.9830352768339, 6.95373319310279, 6.95373319310279, 4.9830352768339, 9.56458610520017, 4.9830352768339, 13.7657907083098, NA, NA), StdDev = c(4.55932029745697, 4.03560271072923, 5.42183783041485, 1.72749876078217, 1.72749876078217, 1, 1.99211042289525, 3.1247047104581, 1, 1, 1.41235641966466, 1, 1.41235641966466, 1.41235641966466, 1, 1.99211042289525, 1, 3.1247047104581, 1.40830867828517, 1.71755640373177)), row.names = c(NA, -20L), class = c("tbl_df", "tbl", "data.frame"))
## shortened your regex a bit
freq$esc <- gsub("^(.*C[0-9]).*", "\\1", freq$vlookup)

db <- data.frame(x = 1:3, y = c(5.5, 6.5, 9.7))
## change the filter so that the example works
Q6 <- freq[grepl("Entrevista_final|1", freq$esc), c("var", "mod", "PERCENT")]

## remove fill = mod from main ggplot2 call and put it into your bar plot and in geom_text as group
ggplot(Q6, aes(x = var, y = PERCENT)) +
  ## use geom_col instead of geom_bar(stat = "identity"), position = "stack" is default
  geom_col(aes(fill = factor(x = mod)), color = "red") +
  geom_text(aes(group = factor(x = mod), label = sprintf(PERCENT, fmt = "%1.0f")),
    position = position_stack(vjust = 0.5)
  ) +
  ## use your coordinate data frame and use the correct geom layer syntax
  geom_point(data = db, aes(x, y))

Created on 2023-02-16 with reprex v2.0.2

tjebo
  • 21,977
  • 7
  • 58
  • 94