题
我想通过标题中的值对我的facet_wrap重新排序。当前,最低值在左上角,最高值(百分比)在右下角。有什么方法可以重新排列构面,以便最高值位于左上角,最低值位于右下角?
当前代码
#Vector of state abbreviations in descending order of percentage difference (calcluated separately)
top_colleges_pct_change <- c('KS', 'NV', 'NY', 'TX', 'OH', 'LA', 'AZ', 'MS', 'OK', "NC", "PA", "CA")
tuition_cost_clean %>%
filter(!is.na(room_and_board),
degree_length == "4 Year",
state_abbr %in% top_colleges_pct_change) %>%
arrange(state, desc(in_state_total)) %>%
group_by(state_abbr) %>%
slice(which.max(in_state_total), which.min(in_state_total)) %>%
mutate(pct_change = round(-diff(in_state_total) / max(in_state_total),3),) %>%
ungroup() %>%
top_n(n = 24, wt = pct_change) %>%
select(name, state_abbr, in_state_total, pct_change) %>%
mutate(label=paste(pct_change * 100,"%")) %>%
ggplot(aes(x = state_abbr, y = in_state_total, fill = name))+
geom_bar(stat = "identity", position = "dodge", width = 0.5)+
facet_wrap(~label, scales = "free")+
labs(title = "States with Largest Difference Between Highest and Lowest Costing 4-Year Colleges\n2018-2019",
x = "State",
y = "In-State Total Cost")+
theme(legend.position = "none",
plot.title = element_text(size = 12))+
scale_y_continuous(labels = scales::dollar)
可复制的数据
tuition_cost_clean <- structure(list(name = c("Aaniiih Nakoda College", "Abilene Christian University",
"Abraham Baldwin Agricultural College", "Academy College", "Academy of Art University",
"Adams State University", "Adelphi University", "Adirondack Community College",
"Adrian College", "Advanced Technology Institute", "Adventist University of Health Sciences",
"Agnes Scott College", "Aiken Technical College", "Aims Community College",
"Alabama Agricultural and Mechanical University", "Alabama Southern Community College",
"Alabama State University", "Alamance Community College", "Alaska Bible College",
"Alaska Pacific University", "Albany College of Pharmacy and Health Sciences",
"Albany State University", "Albany Technical College", "Albertus Magnus College",
"Albion College", "Albright College", "Alcorn State University",
"Alderson-Broaddus University", "Alexandria Technical and Community College",
"Alfred University", "Allan Hancock College", "Allegany College of Maryland",
"Allegheny College", "Allegheny Wesleyan College", "Allen College",
"Allen County Community College", "Allen University", "Alliant International University",
"Alma College", "Alpena Community College", "Alvernia University",
"Alverno College", "Alvin Community College", "Amarillo College",
"Amberton University", "American Academy McAllister Institute of Funeral Service",
"American Academy of Art", "American Academy of Dramatic Arts",
"American Academy of Dramatic Arts: West", "American Baptist College",
"American Indian College of the Assemblies of God", "American International College",
"American Jewish University", "American National University: Charlottesville",
"American National University: Danville", "American National University: Harrisonburg",
"American National University: Lynchburg", "American National University: Martinsville",
"American National University: Salem", "American University SystemFor-profit",
"American River College", "American Samoa Community College",
"American University", "American University of Puerto Rico",
"Amherst College", "Amridge University", "Ancilla College", "Anderson University",
"Anderson University", "Andrew College", "Andrews University",
"Angelina College", "Angelo State University", "Anna Maria College",
"Anne Arundel Community College", "Anoka Technical College",
"Anoka-Ramsey Community College", "Antelope Valley College",
"Antioch College", "Antioch University Los Angeles", "Antioch University Midwest",
"Antioch University Santa Barbara", "Antioch University Seattle",
"Apex School of Theology", "Appalachian Bible College", "Appalachian State University",
"Aquinas College", "Aquinas College", "Arapahoe Community College",
"Arcadia University", "Arizona Christian University", "Arizona State University",
"Arizona Western College", "Arkansas Baptist College", "Arkansas Northeastern College",
"Arkansas State University", "Arkansas State University Mid-South",
"Arkansas State University: Beebe", "Arkansas State University: Mountain Home",
"Arkansas State University: Newport"), state = c("Montana", "Texas",
"Georgia", "Minnesota", "California", "Colorado", "New York",
"New York", "Michigan", "Virginia", "Florida", "Georgia", "South Carolina",
"Colorado", "Alabama", "Alabama", "Alabama", "North Carolina",
"Alaska", "Alaska", "New York", "Georgia", "Georgia", "Connecticut",
"Michigan", "Pennsylvania", "Mississippi", "West Virginia", "Minnesota",
"New York", "California", "Maryland", "Pennsylvania", "Ohio",
"Iowa", "Kansas", "South Carolina", "California", "Michigan",
"Michigan", "Pennsylvania", "Wisconsin", "Texas", "Texas", "Texas",
"New York", "Illinois", "New York", "California", "Tennessee",
"Arizona", "Massachusetts", "California", "Virginia", "Virginia",
"Virginia", "Virginia", "Virginia", "Virginia", "West Virginia",
"California", NA, NA, NA, "Massachusetts", "Alabama", "Indiana",
"South Carolina", "Indiana", "Georgia", "Michigan", "Texas",
"Texas", "Massachusetts", "Maryland", "Minnesota", "Minnesota",
"California", "Ohio", "California", "Ohio", "California", "Washington",
"North Carolina", "West Virginia", "North Carolina", "Michigan",
"Tennessee", "Colorado", "Pennsylvania", "Arizona", "Arizona",
"Arizona", "Arkansas", "Arkansas", "Arkansas", "Arkansas", "Arkansas",
"Arkansas", "Arkansas"), state_abbr = c("MT", "TX", "GA", "MN",
"CA", "CO", "NY", "NY", "MI", "VA", "FL", "GA", "SC", "CO", "AL",
"AL", "AL", "NC", "AK", "AK", "NY", "GA", "GA", "CT", "MI", "PA",
"MS", "WV", "MN", "NY", "CA", "MD", "PA", "OH", "IA", "KS", "SC",
"CA", "MI", "MI", "PA", "WI", "TX", "TX", "TX", "NY", "IL", "NY",
"CA", "TN", "AZ", "MA", "CA", "VA", "VA", "VA", "VA", "VA", "VA",
"WV", "CA", "AS", "DC", "PR", "MA", "AL", "IN", "SC", "IN", "GA",
"MI", "TX", "TX", "MA", "MD", "MN", "MN", "CA", "OH", "CA", "OH",
"CA", "WA", "NC", "WV", "NC", "MI", "TN", "CO", "PA", "AZ", "AZ",
"AZ", "AR", "AR", "AR", "AR", "AR", "AR", "AR"), type = c("Public",
"Private", "Public", "For Profit", "For Profit", "Public", "Private",
"Public", "Private", "For Profit", "Private", "Private", "Public",
"Public", "Public", "Public", "Public", "Public", "Private",
"Private", "Private", "Public", "Public", "Private", "Private",
"Private", "Public", "Private", "Public", "Private", "Public",
"Public", "Private", "Private", "Private", "Public", "Private",
"Private", "Private", "Public", "Private", "Private", "Public",
"Public", "Private", "Private", "Private", "Private", "Private",
"Private", "Private", "Private", "Private", "For Profit", "For Profit",
"For Profit", "For Profit", "For Profit", "For Profit", "Public",
"Public", "Public", "Private", "Private", "Private", "Private",
"Private", "Private", "Private", "Private", "Private", "Public",
"Public", "Private", "Public", "Public", "Public", "Public",
"Private", "Private", "Private", "Private", "Private", "Private",
"Private", "Public", "Private", "Private", "Public", "Private",
"Private", "Public", "Public", "Private", "Public", "Public",
"Public", "Public", "Public", "Public"), degree_length = c("2 Year",
"4 Year", "2 Year", "2 Year", "4 Year", "4 Year", "4 Year", "2 Year",
"4 Year", "2 Year", "4 Year", "4 Year", "2 Year", "2 Year", "4 Year",
"2 Year", "4 Year", "2 Year", "4 Year", "4 Year", "4 Year", "4 Year",
"2 Year", "4 Year", "4 Year", "4 Year", "4 Year", "4 Year", "2 Year",
"4 Year", "2 Year", "2 Year", "4 Year", "4 Year", "4 Year", "2 Year",
"4 Year", "4 Year", "4 Year", "2 Year", "4 Year", "4 Year", "2 Year",
"2 Year", "4 Year", "2 Year", "4 Year", "2 Year", "2 Year", "4 Year",
"4 Year", "4 Year", "4 Year", "2 Year", "2 Year", "2 Year", "2 Year",
"2 Year", "4 Year", "4 Year", "2 Year", "2 Year", "4 Year", "4 Year",
"4 Year", "4 Year", "2 Year", "4 Year", "4 Year", "2 Year", "4 Year",
"2 Year", "4 Year", "4 Year", "2 Year", "2 Year", "2 Year", "2 Year",
"4 Year", "4 Year", "4 Year", "4 Year", "4 Year", "4 Year", "4 Year",
"4 Year", "4 Year", "4 Year", "2 Year", "4 Year", "4 Year", "4 Year",
"2 Year", "4 Year", "2 Year", "4 Year", "2 Year", "2 Year", "2 Year",
"2 Year"), room_and_board = c(NA, 10350, 8474, NA, 16648, 8782,
16030, 11660, 11318, NA, 4200, 12330, NA, NA, 8379, NA, 5422,
NA, 5700, 7300, 10920, 8878, NA, 13200, 12380, 12070, 9608, 8860,
NA, 12516, NA, NA, 12140, 4000, 7282, 5070, 7230, NA, 10998,
NA, NA, 8546, NA, NA, NA, NA, NA, 17955, 13255, 8640, 6250, 14300,
17362, NA, NA, NA, NA, NA, NA, NA, NA, NA, 14880, NA, 14740,
NA, 9600, 9830, 9890, 10636, 9078, 5500, 9130, 14630, NA, NA,
NA, NA, 7640, NA, NA, NA, NA, NA, 7960, 8304, 9332, NA, NA, 13800,
10674, 12648, 6700, 8826, NA, 8160, NA, 5280, NA, NA), in_state_tuition = c(2380,
34850, 4128, 17661, 27810, 9440, 38660, 5375, 37087, 13680, 15150,
41160, 5160, 2281, 9698, 4440, 11068, 2310, 9300, 20830, 35105,
6726, 3246, 32060, 45775, 45306, 7144, 27910, 5416, 33484, 1418,
4140, 47540, 6400, 19970, 3150, 13340, 18000, 40258, 4530, 34885,
28302, 1998, 2670, 12840, 17160, 34100, 35160, 35160, 10950,
958, 35680, 31826, 18735, 18735, 18735, 18735, 18735, 18735,
8150, 1416, 3700, 48459, 6946, 56426, 6900, 17330, 28000, 30450,
17388, 29288, 2625, 8489, 37860, 4110, 5584, 5073, 1420, 35718,
20670, 16210, 22575, 27435, 6200, 14720, 7214, 32574, 23600,
4811, 43580, 26796, 10822, 2520, 8760, 2450, 8608, 3274, 3600,
3570, 3480), in_state_total = c(2380, 45200, 12602, 17661, 44458,
18222, 54690, 17035, 48405, 13680, 19350, 53490, 5160, 2281,
18077, 4440, 16490, 2310, 15000, 28130, 46025, 15604, 3246, 45260,
58155, 57376, 16752, 36770, 5416, 46000, 1418, 4140, 59680, 10400,
27252, 8220, 20570, 18000, 51256, 4530, 34885, 36848, 1998, 2670,
12840, 17160, 34100, 53115, 48415, 19590, 7208, 49980, 49188,
18735, 18735, 18735, 18735, 18735, 18735, 8150, 1416, 3700, 63339,
6946, 71166, 6900, 26930, 37830, 40340, 28024, 38366, 8125, 17619,
52490, 4110, 5584, 5073, 1420, 43358, 20670, 16210, 22575, 27435,
6200, 22680, 15518, 41906, 23600, 4811, 57380, 37470, 23470,
9220, 17586, 2450, 16768, 3274, 8880, 3570, 3480), out_of_state_tuition = c(2380,
34850, 12550, 17661, 27810, 20456, 38660, 9935, 37087, 13680,
15150, 41160, 8010, 13018, 17918, 8880, 19396, 8070, 9300, 20830,
35105, 19802, 5916, 32060, 45775, 45306, 7144, 27910, 5416, 33484,
7898, 9210, 47540, 6400, 19970, 3150, 13340, 18000, 40258, 6840,
34885, 28302, 4818, 5880, 12840, 17160, 34100, 35160, 35160,
10950, 958, 35680, 31826, 18735, 18735, 18735, 18735, 18735,
18735, 8150, 9546, 3700, 48459, 6946, 56426, 6900, 17330, 28000,
30450, 17388, 29288, 5535, 20939, 37860, 12180, 5584, 5073, 9760,
35718, 20670, 16210, 22575, 27435, 6200, 14720, 22021, 32574,
23600, 18671, 43580, 26796, 28336, 9510, 8760, 4250, 15298, 5014,
5760, 5580, 5310), out_of_state_total = c(2380, 45200, 21024,
17661, 44458, 29238, 54690, 21595, 48405, 13680, 19350, 53490,
8010, 13018, 26297, 8880, 24818, 8070, 15000, 28130, 46025, 28680,
5916, 45260, 58155, 57376, 16752, 36770, 5416, 46000, 7898, 9210,
59680, 10400, 27252, 8220, 20570, 18000, 51256, 6840, 34885,
36848, 4818, 5880, 12840, 17160, 34100, 53115, 48415, 19590,
7208, 49980, 49188, 18735, 18735, 18735, 18735, 18735, 18735,
8150, 9546, 3700, 63339, 6946, 71166, 6900, 26930, 37830, 40340,
28024, 38366, 11035, 30069, 52490, 12180, 5584, 5073, 9760, 43358,
20670, 16210, 22575, 27435, 6200, 22680, 30325, 41906, 23600,
18671, 57380, 37470, 40984, 16210, 17586, 4250, 23458, 5014,
11040, 5580, 5310)), row.names = c(NA, -100L), class = c("tbl_df",
"tbl", "data.frame"))
更改为一个因素,将您的label
创作更改为
mutate(
label = paste(pct_change * 100,"%"),
label = factor(label, levels = unique(label[order(-pct_change)])),
) %>%
完整代码:
tuition_cost_clean %>%
filter(!is.na(room_and_board),
degree_length == "4 Year",
state_abbr %in% top_colleges_pct_change) %>%
arrange(state, desc(in_state_total)) %>%
group_by(state_abbr) %>%
slice(which.max(in_state_total), which.min(in_state_total)) %>%
mutate(pct_change = round(-diff(in_state_total) / max(in_state_total),3),) %>%
ungroup() %>%
top_n(n = 24, wt = pct_change) %>%
select(name, state_abbr, in_state_total, pct_change) %>%
## start modified code
mutate(
label = paste(pct_change * 100,"%"), # (unchanged)
label = factor(label, levels = unique(label[order(-pct_change)])),
) %>%
## end modified code
ggplot(aes(x = state_abbr, y = in_state_total, fill = name))+
geom_bar(stat = "identity", position = "dodge", width = 0.5)+
facet_wrap(~label, scales = "free")+
labs(title = "States with Largest Difference Between Highest and Lowest Costing 4-Year Colleges\n2018-2019",
x = "State",
y = "In-State Total Cost")+
theme(legend.position = "none",
plot.title = element_text(size = 12))+
scale_y_continuous(labels = scales::dollar)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句