Tidily evaluated ggplot2

July 3, 2018
By

[This article was first published on HighlandR, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Test driving the new release of ggplot2 –

A new release of ggplot2

Now that tidy evaluation is baked into ggplot2, as of TODAY, let’s take it for a spin:

ggplot2 3.0.0 %>%
create function %>%
test function %>%
end

library(dplyr)
library(ggplot2)
library(tidyr)
library(tibble)

data <- list(fdeaths,mdeaths,ldeaths) #time series data- needs prep

names(data)[1:3] <- c("fdeaths","mdeaths","ldeaths")

data <- as_tibble(data)

startdate <- as.Date('1974-1-1')

data$date <- seq.Date(startdate,by = 'month',length.out = 72)


newdata <- tidyr::gather(data, key = key, value = value,-date)

newdata$value <- as.numeric(newdata$value)

# create generic function

gtest <- function(df,x,y, group) {
  
  
  x_quo <- enquo(x)
  
  y_quo <- enquo(y)
  
  group_quo <- enquo(group)
  
  
  
  p <- ggplot(df,aes(x = !!x_quo, y = !!y_quo)) +   #bangin'   
    
    geom_line(colour = "blue", group = 1) + 
    
    geom_point(colour = "blue") +
    
    facet_wrap(group_quo, ncol = 3) #look Ma, no need to bang bang here!
  
  
  p <- p + ggtitle(label = "Easy Tidy Eval in ggplot 3.0.0",
                   
                   subtitle = "ggplot with tidy evaluation & facetting with no strings")
  
  p <- p + labs(x = NULL, y = NULL, caption = "") +
    theme_bw()
  
  p
}

Let’s test it:

gtest(newdata,date,value,key)

2018-07-04-tidy-string-free-ggplot.png

End

To leave a comment for the author, please follow the link and comment on their blog: HighlandR.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)