ggplot中的订单类别

时间:2019-08-23 06:30:15

标签: r ggplot2 forcats

问题

我想知道如何使用fct_recodegeom_col图中变量的位置进行重新排序。目的是根据澳大利亚的价值观从最大到最小,从下到上排序。

enter image description here

我的尝试

ggplot(data = df, aes(x = country, y = value, fill = fct_recode(chart_type, value)) + 
  geom_col()

数据

structure(list(country = c("Australia", "Australia", "Australia", 
"Australia", "Australia", "Australia", "Australia", "Australia", 
"Australia", "Australia", "Australia", "Australia", "Australia", 
"Australia", "Australia", "Australia", "Australia", "Australia", 
"Australia", "OECD - Average", "OECD - Average", "OECD - Average", 
"OECD - Average", "OECD - Average", "OECD - Average", "OECD - Average", 
"OECD - Average", "OECD - Average", "OECD - Average", "OECD - Average", 
"OECD - Average", "OECD - Average", "OECD - Average", "OECD - Average", 
"OECD - Average", "OECD - Average", "OECD - Average", "OECD - Average"
), value = c(11.314258, 4.572229, 0, 0, 1.356282, 1.665903, 0, 
0, 1.330865, 0, 0, 3.57475, 0.086851, 0, 2.786991, 0, 1.069969, 
0, 0, 8.239819, 2.865435, 0.241395, 9.160657, 0.400194, 1.102444, 
0.167023, 0.12324, 0.435496, 0.051199, 0.012553, 6.792114, 0.120639, 
0.073286, 3.288726, 0.002667, 0.6529, 0.043262, 0.157044), chart_type = c("Income tax", 
"Corporation tax", "Other taxes", "SSC's + payroll tax", "SSC's + payroll tax", 
"Recurrent building taxes", "Other taxes on property/capital", 
"Other taxes on property/capital", "Other taxes on property/capital", 
"Other taxes on property/capital", "Other taxes on property/capital", 
"VAT and GST", "VAT and GST", "Other indirect taxes", "Other indirect taxes", 
"Other indirect taxes", "Other indirect taxes", "Other indirect taxes", 
"Other taxes", "Income tax", "Corporation tax", "Other taxes", 
"SSC's + payroll tax", "SSC's + payroll tax", "Recurrent building taxes", 
"Other taxes on property/capital", "Other taxes on property/capital", 
"Other taxes on property/capital", "Other taxes on property/capital", 
"Other taxes on property/capital", "VAT and GST", "VAT and GST", 
"Other indirect taxes", "Other indirect taxes", "Other indirect taxes", 
"Other indirect taxes", "Other indirect taxes", "Other taxes"
)), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-38L))

1 个答案:

答案 0 :(得分:0)

你的意思是这样吗?

Id IdentifierString
-------------------
1  NULL
2  NULL
3  String D

enter image description here