如何使用以下R代码和ggplot2包来复制下面的图形?

5

使用我的数据:

Row | Year | SchoolID | SchoolName | BudgetArea |PaymentPerStudent

001   2011     ABC       PS #1         Staff            12000                
002   2012     ABC       PS #1         Staff            10000
003   2011     ABC       PS #1         Lunch            22000
004   2012     ABC       PS #1         Lunch            18000 
005   2011     DEF       PS #2         Staff            80000
006   2012     DEF       PS #2         Staff            65000
007   2013     DEF       PS #2         Staff            50000
008   2011     DEF       PS #2         Lunch            23000
009   2012     DEF       PS #2         Lunch            34000
010   2013     DEF       PS #2         Lunch            28000
011   2011     GHI       PS #3         Staff             9000
012   2012     GHI       PS #3         Staff            10000
013   2013     GHI       PS #3         Staff            12000
014   2011     GHI       PS #3         Lunch            22000
015   2012     GHI       PS #3         Lunch            17000
016   2013     GHI       PS #3         Lunch            18000

我希望重现以下图表:

期望的ggplot2图片

绘图和R代码来源

其中:

1)“GradeA”... “GradeN”的值要替换为“SchoolName”的值。

2)“Group”值(如Apples,Bananas等)将被替换为“Budget Area”值(如人员、午餐等)。

3)“Proportion Tasty”值要替换为“PaymentPerStudent”值。

编辑(04/09/2014):我已经尝试了以下内容,并得到了Jaap的输入(请参见下文):

    ggplot(data=Rates_2, aes(x=factor(Year), y=PaymentPerStudent/max(PaymentPerStudent), 
                         group=BudgetArea, shape=BudgetArea, color=BudgetArea)) + 
  geom_line() + 
  geom_point() +
  labs(title = "Pay rate per student by year, budget area, and school") +
  scale_x_discrete("Year") +
  scale_y_continuous("PaymentPerStudent", limits=c(0,1)) +
  facet_grid(.~SchoolID)

然而,它生成以下“压缩”的图表:
我希望找到一种方法,在结果图中将学校(每页可能有9所学校)分割到不同的页面上,以便更好地理解这些图表。
请注意:
1)数据框具有将近2,000行数据,代表400多所学校。
2)时间跨度为2001年至2004年。
3)每个学生支付的变量范围从10,000到100,000。 我希望重新调整变量(使其介于0和1之间),以实现我的制图目标。
1个回答

4
您在facet_grid前忘记了+。提供的示例数据包含2011年、2012年和2013年。因此,我保留了这些数据。以下是代码:
ggplot(data=Rates_2, aes(x=factor(Year), y=PaymentPerStudent/max(PaymentPerStudent), 
                         group=BudgetArea, shape=BudgetArea, color=BudgetArea)) + 
  geom_line() + 
  geom_point() +
  labs(title = "Pay rate per student by year, budget area, and school") +
  scale_x_discrete("Year") +
  scale_y_continuous("PaymentPerStudent", limits=c(0,1)) +
  facet_grid(.~SchoolID)

运行这段代码会得到如下结果:enter image description here


如果想要为每个学校生成单独的图表,可以使用以下代码:

# get the max value so you compare the plots better
max(Rates_2$PaymentPerStudent)

# split the dataframe into a list of dataframes for each school
dfs <- split(Rates_2, Rates_2$SchoolID)

# make a plot for each school
lapply(dfs, function(df) ggplot(df, aes(x=factor(Year), y=PaymentPerStudent/80000, 
                                                  group=BudgetArea, shape=BudgetArea, color=BudgetArea)) + 
         geom_line() + geom_point() +
         labs(title = "Pay rate per student by year, budget area, and school") +
         scale_x_discrete("Year") +
         scale_y_continuous("PaymentPerStudent", limits=c(0,1))
)

鉴于学校数量众多,使用您的代码生成的结果会产生一个“压缩”的图表(请参见上文)。 - ealfons1
是的,你的第二个回应产生了期望的输出。 - ealfons1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接