Home > front end >  ggplot does not display inside a function
ggplot does not display inside a function

Time:09-29

I wrote this function to plot the city and income. The plotting code works perfectly fine when it is not inside the function. However, when I called it with the function, no graph showed up. Please help! Thanks.

graph <- function(x, y) {
  df %>% 
    filter(city == "x", year == y) %>% 
    ggplot(aes(log(income)))  
      geom_histogram()
}

I tried to assign the plot to p and then add print(p), but nothing showed up.

CodePudding user response:

you take x as a param, but dont use it in your function; you use a hardcoded x to check against city; its likely you get nothing, and so plot empty charts

besides from that, I can set up an analogous construction and see charts just fine; so theres not inherent reason that the plot wouldnt be seen from inside a function like this

  • Related