Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More Purrr for GGPlot2 #1

Open
jdnewmil opened this issue Mar 30, 2017 · 1 comment
Open

More Purrr for GGPlot2 #1

jdnewmil opened this issue Mar 30, 2017 · 1 comment

Comments

@jdnewmil
Copy link

Thanks to http://r4ds.had.co.nz/ I can riff on your idea and point out that the "do" function doesn't play well with other columns. A little more purrr and some tidyr and you can work with many list columns.

library(ggpmisc) # for annotation
library(tidyr) # for nest
plots <- small_pwt %>%
         mutate( country = as.character( country ) ) %>%
         group_by(country) %>%
         nest %>%
         mutate( avhlm = map( data
                            , function( DF ) {
                                lm( avh ~ year, data = DF )
                              }
                            )
               ) %>%
         mutate( plot = pmap( .
                            , function( country, data, avhlm ) { 
                                ggplot( data, aes( y = avh, x = year ) ) + 
                                  theme_tufte() +
                                  geom_line() +
                                  geom_smooth( method = "lm" ) +
                                  stat_poly_eq( formula = y ~ x
                                              , aes( label = paste( ..eq.label..
                                                                  , ..rr.label..
                                                                  , sep = "*\",\"~~~"
                                                                  )
                                                   )
                                              , label.y.npc = "bottom"
                                              , parse = TRUE
                                              ) +
                                  ggtitle( country ) +
                                  ylab( "Year") +
                                  xlab( "Average annual hours worked by persons engaged")
                              }
                            )
               )

# RStudio keeps a history you can surf through
walk( plots$plot, print )
@b-rodrigues
Copy link
Member

Hey thanks for the message! For some reason I've completely forgot to answer, and then I forgot all about it :/ Shame on me!

In any case thanks, I'll study your code, there's some stuff I don't understand yet (like stat_poly_eq, and everything that follows... seems useful!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants