Good resources for programming with ggplot2?

Does anyone have any resources for learning how to write functions using ggplot calls? Is the ggplot2 book by @hadley my best bet?

I have barely written any functions but the few I have done actually include ggplot graphs. I found that the approach was essentially the same as any function - passing the name of a dataframe and a variable by which I needed to filter.

I do not have access to my code at the moment, but I'll flag this topic and try to post an example when I am back at my work computer. In the meantime, others will probably give you much more helpful advice :smile:

Danielle

Hey Danielle,

I was trying to pass a data frame created in a set of dplyr pipes into a ggplot call like so:

Prop_Chart <- function(data, grp.var, factor_title) {
grp.var <- enquo(grp.var)
tmp <- data %>%
group_by(!!grp.var) %>%
summarise(n = n()) %>%
mutate(prop = n / sum(n))
ggplot(tmp, aes(x = prop, y = fct_reorder(eval(rlang::!!(grp.var)), prop, .desc = TRUE))) + geom_point(colour = 'blue') +
labs(x = "% of Total", y = factor_title)
}

The code works but I was just wondering if anyone had some recommendations on blogs/readings that could help explain how best to use function args like above.

I would suggest using ggplot2::stat_summary. an example of something similar to what you are doing is in the help for the function (?ggplot2::stat_summary)

# A set of useful summary functions is provided from the Hmisc package:
stat_sum_df <- function(fun, geom="crossbar", ...) {
  stat_summary(fun.data = fun, colour = "red", geom = geom, width = 0.2, ...)
}
d <- ggplot(mtcars, aes(cyl, mpg)) + geom_point()
# The crossbar geom needs grouping to be specified when used with
# a continuous x axis.
d + stat_sum_df("mean_cl_boot", mapping = aes(group = cyl))
d + stat_sum_df("mean_sdl", mapping = aes(group = cyl))
d + stat_sum_df("mean_sdl", fun.args = list(mult = 1), mapping = aes(group = cyl))
d + stat_sum_df("median_hilow", mapping = aes(group = cyl))
2 Likes

I'd start at http://rpubs.com/hadley/97970

3 Likes

@hadley Thanks, I'll check it out.

Adan - Here's the example I promised...it is one of the first functions I did. For my work I have occasion to analyze survey information. It's not really all that different from what is in Hadley's rpub page so I guess that's a good sign for me.

The call is average_and_plot_geo_smooth(surveys_to_include, prod_var)

and the function is...

average_and_plot_geo_smooth <- function (df, prod_group){

    df %>% filter (!is.na(major_geo)) %>% 
            select (case_number, ends_with("sat"), product_csat, day_week_year, major_geo) %>% 
            group_by(day_week_year, major_geo) %>% 
            summarize (avg_ttr_sat = mean(ttr_sat, na.rm = TRUE),
                       avg_knowledge_sat = mean(tse_knowledge_sat, na.rm=TRUE))%>%
            gather (key = metric, value = measurement, avg_ttr_sat, avg_knowledge_sat) %>% 
            ggplot (aes(x=day_week_year, y= measurement, color = major_geo)) +
            stat_smooth(method = "loess", span=0.50, n = 10, na.rm=TRUE, se=FALSE) +
            facet_wrap (~metric) +
            scale_color_manual(values = c("red", "purple", "blue")) +
            labs (x = "Month", y="Smoothed Score", color = "Contact Geo")
1 Like