[R-sig-Geo] merge ggplots into one with pipeline commands

Marta Rufino m@rt@@m@ru||no @end|ng |rom gm@||@com
Wed Feb 3 17:10:17 CET 2021


Hi,

I want to make one only ggplot by merging/joining a base ggplot and a
ggplot resulting from a pipeline command (ddplyr).

Here it is the reproducible example:

# my base plot, for example:
# https://rpubs.com/MRufino/Portugal
p1 <- ggplot() +
    borders("world",
            fill="antiquewhite1",
            colour="antiquewhite4")+
  coord_fixed(ylim=c(36.6, 42.0), xlim=c(-10 ,-7))+
  theme_minimal()+
  theme(panel.background = element_rect(fill = 'powderblue'),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank())+
  xlab("Longitude")+ ylab("Latitude")
 p1

# now, if I want to add this plot to another ggplot resulting from a
pipeline command, how can I do it?
dat <- data.frame(lat=c(40.3,38.4,39),
           lon = c(-10,-8,-9),
           fac = c(1,2,1))

# pipeline ggplot
dat %>%
  filter(fac==1) %>%
  ggplot()+
  geom_point(aes(x=lon, y=lat))

# I want to obtain this, but just using the pipeline command (because in my
case both plots are complex and long and need to be repeated).
p1 +
  geom_point(data=dat[dat$fac==1,],
             aes(x=lon, y=lat))

# ideally something like this (which does not work)
dat %>%
  filter(fac==1) %>%
  ggplot()+
  geom_point(aes(x=lon, y=lat))+
  p1

# OR like this...
dat %>%
  filter(fac==1) %>%
  p1+
  geom_point(aes(x=lon, y=lat))

I am sure I am missing something and should be simple, but although I
searched I could not find any post about it.

Any help will be much apretiated,
Thank you very much in advance,

Cheers,
M.

-- 
Marta M. Rufino (junior researcher)

*_____________________________________**Portuguese Institute for the Sea
and the Atmosphere (IPMA)*
Division of Modelling and Management of Fisheries Resources
Av. Dr. Alfredo Magalhães Ramalho, 6, 1495-165 Lisboa

*Centre of Statistics and its Applications (CEAUL) *

Faculty of Sciences, Univ. of Lisbon, Portugal

http://rpubs.com/MRufino/

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list