将参数传递给ggplot和facet_grid

时间:2016-02-24 15:43:16

标签: r ggplot2

我需要这些代码行的帮助。

我的数据集

> 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 ...

我希望'

的参考代码
 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')

Correct Plot

我想在上面的示例中替换data.tidy,InfantMortality,IncomeGroup和title。

以下是我对代码的尝试:

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')
}

错误:

> 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

我尝试了aes_string,但我无法让它工作。错误是什么意思?我该如何解决这个问题?

更新 我现在有一些部分工作的代码,使用 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. 
> 

仍然是一个不正确的情节&gt; Incorrect Plot

提前谢谢你, 拉胡尔

1 个答案:

答案 0 :(得分:0)

我有解决方案。三个步骤对我有用:   - 将data.tidy中Year变量的数据类型从factor更改为numeric。   - 使用aes_string作为ggplot参数   - 对于facet_grid(),许多工作都有效:

  • 使用as.formula()传递'〜IncomeGroup'
  • 只需将'〜IncomeGroup'直接传递给facet_grid()
  • 即可

最终代码:

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')