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

DELORME Michael - DRAAF-AUVERGNE-RHONE-ALPES/SRISET/PEVAD m|ch@e|@de|orme @end|ng |rom @gr|cu|ture@gouv@|r
Fri Feb 5 08:05:17 CET 2021


Use ".", not ".data" and "lat" not "y" :

library(tidyverse)

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

dat <- data.frame(lat = c(40.3,38.4,39),
                   lon = c(-10,-8,-9),
                   fac = c(1,2,1))

dat %>%
   filter(fac == 1) %>%
   { p1 +
       geom_point(data = ., aes(x = .$lon, y = .$lat)) +
       coord_fixed(ylim = c(min(.$lat), max(.$lat)),
                   xlim = c(min(.$lon), max(.$lon))) +
       geom_point()
   }


Le 05/02/2021 03:33, > marta.m.rufino a écrit :
> Hi.
> 
> Thank you so much!
> Excellent! It works wonderfully.
> It will be very useful.
> 
> Should I abuse of your knowledge, take advantage of this example and 
> ask if
> you or anyone knows how to limit the map range to the area of the 
> datasets
> 'dat', without saving xlim/ylim outside the plot?
> 
> i.e. making p1 automatically limited to the geographic range of dat, 
> inside
> the ggplot code (and not plotting always the complete country)?
> 
> like this, but automatically within the ggplot code (I have to make 
> many
> maps, each with different areas):
> 
> # this is what I would like to get:
> ggplot(data = dat, aes(x=lon, y=lat)) +
>     borders("world",fill="antiquewhite1",colour="antiquewhite4")+
>   coord_fixed(ylim=c(min(dat$lat), max(dat$lat)),xlim=c(min(dat$lon) ,
> max(dat$lon)))+
> geom_point()
> 
> # however, with pipeline, I would need to set xlim and ylim outside the
> plot code
> ggplot(data = dat, aes(x=lon, y=lat)) +
>     borders("world",fill="antiquewhite1",colour="antiquewhite4")+
>   coord_fixed(ylim=c(min(.data[[lat]]), max(.data[[lat]])),xlim=c(min(.
> data[[lon]]) , max(.data[[lon]])))+
> geom_point()
> #
> https://stackoverflow.com/questions/4856849/looping-over-variables-in-ggplot
> 
> # this does not work either (if I use dat it refers to all dat, not 
> only
> when fac==1):
> dat %>%
>   filter(fac==1) %>%
>   { p1 +
>     geom_point(data=., aes(x=lon, y=lat))+
> coord_fixed(ylim=c(min(data$y), max(data$y)),xlim=c(min(data$lon) , 
> max(
> data$lon)))+
> geom_point()
>   }
> #
> https://stackoverflow.com/questions/44889239/unable-to-pass-the-ylim-argument-to-ggplot-in-a-function
> 
> Thank you very much once again!
> Best regards,
> M.
> 
> Kent Johnson <kent3737 using gmail.com> escreveu no dia quinta, 4/02/2021 
> à(s)
> 19:43:
> 
>> On Thu, Feb 4, 2021 at 6:01 AM <r-sig-geo-request using r-project.org> wrote
>> 
>>> Message: 3
>>> Date: Wed, 3 Feb 2021 16:10:17 +0000
>>> From: Marta Rufino <marta.m.rufino using gmail.com>
>>> To: r-sig-geo <r-sig-geo using r-project.org>
>>> Subject: [R-sig-Geo] merge ggplots into one with pipeline commands
>>> Message-ID:
>>>         <
>>> CAKSASLDyVVWgDJ6VT1ZWUEBoh8KbXufa+bv1UePi0YUABOz9CQ using mail.gmail.com>
>>> Content-Type: text/plain; charset="utf-8"
>>> 
>>> 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))
>>> 
>> 
>> This will do it. Enclosing the pipe expression in { } lets you put the
>> piped-in variable where you need it using '.'.
>> 
>> dat %>%
>>   filter(fac==1) %>%
>>   { p1 +
>>     geom_point(data=., aes(x=lon, y=lat))
>>   }
>> 
>> Kent Johnson
>> Cambridge, MA
>> 
> 
> 	[[alternative HTML version deleted]]
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list