gpt4 book ai didi

r - 如何使用以下 R 代码通过 ggplot2 包重现以下图?

转载 作者:行者123 更新时间:2023-12-03 16:44:34 24 4
gpt4 key购买 nike

用我的数据:

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

我想重现以下内容:

Desired ggplot2

Source for plot and R code

哪里:

1) Grade A.....Grade N 值替换为“SchoolName”值

2) Group 值(Apples、Bananas 等)被替换为“Budget Area”值(Staff、Lunch 等)

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)

但是,它会产生以下“浓缩”图:

Condensed Plot

我想找到一种将学校(每页可能有 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)

给我这个结果:
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))
)

关于r - 如何使用以下 R 代码通过 ggplot2 包重现以下图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22972601/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com