{"action":"created","issue":{"url":"https://api.github.com/repos/tidyverse/ggplot2/issues/2506","repository_url":"https://api.github.com/repos/tidyverse/ggplot2","labels_url":"https://api.github.com/repos/tidyverse/ggplot2/issues/2506/labels{/name}","comments_url":"https://api.github.com/repos/tidyverse/ggplot2/issues/2506/comments","events_url":"https://api.github.com/repos/tidyverse/ggplot2/issues/2506/events","html_url":"https://github.com/tidyverse/ggplot2/issues/2506","id":309920770,"number":2506,"title":"Better pipe support","user":{"login":"foldager","id":5124332,"avatar_url":"https://avatars0.githubusercontent.com/u/5124332?v=4","gravatar_id":"","url":"https://api.github.com/users/foldager","html_url":"https://github.com/foldager","followers_url":"https://api.github.com/users/foldager/followers","following_url":"https://api.github.com/users/foldager/following{/other_user}","gists_url":"https://api.github.com/users/foldager/gists{/gist_id}","starred_url":"https://api.github.com/users/foldager/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/foldager/subscriptions","organizations_url":"https://api.github.com/users/foldager/orgs","repos_url":"https://api.github.com/users/foldager/repos","events_url":"https://api.github.com/users/foldager/events{/privacy}","received_events_url":"https://api.github.com/users/foldager/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-03-29T22:14:41Z","updated_at":"2018-04-19T07:59:20Z","closed_at":"2018-04-19T07:59:20Z","author_association":"NONE","body":"ggplot uses addition between the different elements. I see the argument against using a the pipe operator, especially because ggplot does not use function composition. However, I find piping into ggplot a  a bit awkward, and I propose that a kind of sum function is added that makes piping cleaner. The piping is especially awkward when the return value is piped further on.\r\n\r\n\r\n\r\nIn the examples below I use print as a placeholder function, this could be a function that handled saving the plot.\r\n\r\n    plotting_function <- print\r\n\r\nExample using plain ggplot. Note the ugly parentheses needed because of the precedence of pipe over plus operators.\r\n\r\n    # Parentheses needed. Ends after ggtitle()\r\n    (mtcars %>% \r\n        ggplot() +\r\n            aes(x=hp, y=mpg) +\r\n            geom_point() +\r\n            geom_smooth() +\r\n            ggtitle('Fantastic Plot')) %>%\r\n        plotting_function()\r\n\r\n\r\nExample implementation:\r\n\r\n    ggplot_sum <- function(data, ...){\r\n        p <- ggplot(data)\r\n        args = list(...)\r\n        for (arg in args){\r\n            p <- p + arg\r\n        }\r\n        return (p)\r\n    }\r\n\r\nWhich allow you to nicely group the ggplotting\r\n\r\n    mtcars %>% \r\n        ggplot_sum(\r\n            aes(x=hp, y=mpg),\r\n            geom_point(),\r\n            geom_smooth(),\r\n            ggtitle('Fantastic Plot')) %>%\r\n        plotting_function()\r\n\r\nI think it is a good call to not include the pipe operator as rejected in #2326, but I still think the interaction with the tidyverse could be improved. An alternative to the above is to use `add` from `magrittr`\r\n\r\n    mtcars %>% \r\n        ggplot() %>%\r\n        add(aes(x=hp, y=mpg)) %>%\r\n        add(geom_point()) %>%\r\n        add(geom_smooth()) %>%\r\n        add(ggtitle('Fantastic Plot')) %>%\r\n        plotting_function()\r\n\r\nI think the `ggplot_sum` is much cleaner and I like how it groups plotting elements together."},"comment":{"url":"https://api.github.com/repos/tidyverse/ggplot2/issues/comments/382645961","html_url":"https://github.com/tidyverse/ggplot2/issues/2506#issuecomment-382645961","issue_url":"https://api.github.com/repos/tidyverse/ggplot2/issues/2506","id":382645961,"user":{"login":"batpigandme","id":831732,"avatar_url":"https://avatars0.githubusercontent.com/u/831732?v=4","gravatar_id":"","url":"https://api.github.com/users/batpigandme","html_url":"https://github.com/batpigandme","followers_url":"https://api.github.com/users/batpigandme/followers","following_url":"https://api.github.com/users/batpigandme/following{/other_user}","gists_url":"https://api.github.com/users/batpigandme/gists{/gist_id}","starred_url":"https://api.github.com/users/batpigandme/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/batpigandme/subscriptions","organizations_url":"https://api.github.com/users/batpigandme/orgs","repos_url":"https://api.github.com/users/batpigandme/repos","events_url":"https://api.github.com/users/batpigandme/events{/privacy}","received_events_url":"https://api.github.com/users/batpigandme/received_events","type":"User","site_admin":false},"created_at":"2018-04-19T07:59:20Z","updated_at":"2018-04-19T07:59:20Z","author_association":"OWNER","body":"Please see Hadley's explanation here:\r\nhttps://community.rstudio.com/t/why-cant-ggplot2-use/4372/7"}}
{"actor":{"display_login":"batpigandme"}}