简体   繁体   中英

Passing arguments to ggplot and facet_grid

I need some help with these lines of code.

My data set:

> str(data.tidy)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   9480 obs. of  11 variables:
 $ Country.Name     : Factor w/ 248 levels "Afghanistan",..: 234 12 13 20 22 31 17 16 25 28 ...
 $ Country.Code     : Factor w/ 248 levels "ABW","AFG","AGO",..: 7 12 13 16 17 18 19 21 27 28 ...
 $ Year             : Factor w/ 56 levels "1960","1961",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ InfantMortality  : num  137.3 20.3 37.3 29.5 186.9 ...
 $ AdolFertilityRate: num  176.9 44.8 48.4 27.1 85.8 ...
 $ FertilityRate    : num  6.93 3.45 2.69 2.54 6.28 ...
 $ LifeExpectancy   : num  52.2 70.8 68.6 69.7 37.3 ...
 $ TotalUnemp       : num  NA NA NA NA NA NA NA NA NA NA ...
 $ TotalPop         : num  92612 10276477 7047539 9153489 2431620 ...
 $ Region           : Factor w/ 8 levels "","East Asia & Pacific",..: 5 2 3 3 8 8 7 5 4 4 ...
 $ IncomeGroup      : Factor w/ 6 levels "","High income: nonOECD",..: 2 3 3 3 4 4 5 2 5 6 ...

Reference code that I want to 'functionize':

 ggplot(data.tidy,aes(as.numeric(as.character(Year)),y=InfantMortality))+
    geom_line(aes(color=Country.Name))+
    facet_grid(.~IncomeGroup)+
    theme(legend.position="none")+
    theme(strip.text.x = element_text(size = 7))+
    labs(x='Year', title='Change in mortality rate over time')+
    geom_smooth(color='black')

正确的情节


I want to replace data.tidy, InfantMortality, IncomeGroup and title in the example above.

Here was my attempt at the code:

facetedlineplot <- function(df,y,facet,title){
  ggplot(df,aes(as.numeric(as.character(Year)),y=y))+
    geom_line(aes(color=Country.Name))+
    facet_grid(.~facet)+
    theme(legend.position="none")+
    theme(strip.text.x = element_text(size = 7))+
    labs(x='Year',title=title)+
    geom_smooth(color='black')
}

The error:

> facetedlineplot(data.tidy,y = 'InfantMortality',facet = 'IncomeGroup',title = 'Title goes here')
  Error in layout_base(data, cols, drop = drop) : 
  At least one layer must contain all variables used for facetting

I have tried aes_string, but I couldn't get it to work. What does the error mean? How can I work around this issue?

Update: I have some code that partially works now, using reformulate()

facetedlineplot <- function(df,y,facet,title){
  year <- as.numeric(as.character(df$Year))
  ggplot(df,aes(x=year,y=y))+
    geom_line(aes(color=Country.Name))+
    facet_grid(paste('.~',reformulate(facet)))+
    theme(legend.position="none")+
    theme(strip.text.x = element_text(size = 7))+
    labs(x='Year',title=title)+
    geom_smooth(color='black')
}


> facetedlineplot(data.tidy,y = 'InfantMortality', facet = 'IncomeGroup', title = 'Title goes here')
Warning message:
Computation failed in `stat_smooth()`:
x has insufficient unique values to support 10 knots: reduce k. 
> 

Still, an incorrect plot> 不正确的情节

Thank you in advance, Rahul

I have the solution. Three steps worked for me: - Change datatype of the Year variable in data.tidy from factor to numeric. - Use aes_string for the ggplot argument - For facet_grid(), many things worked:

  • Use as.formula() to pass '~IncomeGroup'
  • Just pass '~IncomeGroup' directly to facet_grid()

Final code:

facetedlineplot <- function(df,y,facet,title){
  ggplot(df,aes_string(x = 'Year', y = y))+
    geom_line(aes(color=Country.Name))+
    facet_grid(facet)+
    theme(legend.position="none")+
    theme(strip.text.x = element_text(size = 9))+
    labs(x='Year',title=title)+
    geom_smooth(color='black')
}
d <- data.tidy
d$Year <- as.numeric(as.character(d$Year))
facetedlineplot(d,'InfantMortality','~IncomeGroup','Title')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM