用我的数据:
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
Run Code Online (Sandbox Code Playgroud)
我想重现以下内容:
哪里:
1)A级...... N级值被"SchoolName"值替换
2)集团价值(苹果,香蕉等)被"预算区"值(员工,午餐等)取代
3)比例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)
Run Code Online (Sandbox Code Playgroud)
但是,它会产生以下"浓缩"情节:
我想找到一种方法将学校(每页可能有9所学校)拆分到最终绘图的不同页面上,以便绘图可以理解.
请注意:
1)数据框有不到2,000行的数据,代表着400多所学校.
2)年份的年份是2001年至2004年.
3)PaymentPerStudent变量的范围从10,000到100,000.我想重新调整变量(介于0和1之间)以实现我制作这些图的目标.
你忘了+
之前的事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)
Run Code Online (Sandbox Code Playgroud)
给了我这个结果:
为了获得每所学校的单独绘图,您可以使用:
# 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))
)
Run Code Online (Sandbox Code Playgroud)