在不使用ggplot bar plot的组内重新排序

在不使用ggplot bar plot的组内重新排序,r,ggplot2,dplyr,R,Ggplot2,Dplyr,我举了一个例子: 这是我的top5_变量的dput structure(list(iso3_code = c("BGD", "CHN", "CHN", "CHN", "DEU", "EGY", "IDN", "IDN", "IND", "IND", "JPN", "KOR", "MEX", "MEX", "MMR", "NLD", "NLD", "PAK", "VNM", "VNM"), country = c("Bangladesh", "China", "China", "China

我举了一个例子:

这是我的top5_变量的dput

structure(list(iso3_code = c("BGD", "CHN", "CHN", "CHN", "DEU", 
"EGY", "IDN", "IDN", "IND", "IND", "JPN", "KOR", "MEX", "MEX", 
"MMR", "NLD", "NLD", "PAK", "VNM", "VNM"), country = c("Bangladesh", 
"China", "China", "China", "Germany", "Egypt", "Indonesia", "Indonesia", 
"India", "India", "Japan", "South korea", "Mexico", "Mexico", 
"Myanmar", "Netherlands", "Netherlands", "Pakistan", "Viet Nam", 
"Viet Nam"), source_crop = c("Sugar cane", "Oil palm fruit", 
"Soybeans", "Sugar cane", "Oil palm fruit", "Maize", "Soybeans", 
"Sugar cane", "Oil palm fruit", "Sugar cane", "Maize", "Maize", 
"Maize", "Soybeans", "Sugar cane", "Oil palm fruit", "Soybeans", 
"Oil palm fruit", "Maize", "Soybeans"), FC_imports = c(1949020L, 
5457164L, 84505368L, 3062047L, 2201599L, 8652235L, 6311197L, 
4761885L, 8335597L, 2119435L, 15342352L, 9804926L, 14217581L, 
6540675L, 2205780L, 3867813L, 7534711L, 2817298L, 8228098L, 6273590L
)), row.names = c(NA, -20L), .internal.selfref = <pointer: 0x7f953580b2e0>, class = c("tbl_df", 
"tbl", "data.frame"))
这是我的输出


我希望从每个组中对条进行排序,但我无法使代码正常工作。我做错了什么?

您需要重新排序变量:

plot_data %>%  
  ggplot(aes(reorder(y = FC_imports, x = country, fill = source_crop), FC_imports)) +
  geom_bar(stat="identity") +
  facet_wrap(~source_crop, scales = "free") +
  scale_x_discrete(breaks = plot_data$order, labels = plot_data$country) +
  coord_flip()

您需要对变量重新排序:

plot_data %>%  
  ggplot(aes(reorder(y = FC_imports, x = country, fill = source_crop), FC_imports)) +
  geom_bar(stat="identity") +
  facet_wrap(~source_crop, scales = "free") +
  scale_x_discrete(breaks = plot_data$order, labels = plot_data$country) +
  coord_flip()

您确定您的
aes
?我认为有一个括号放错了位置。是的,有点错误,因为我得到了这个错误:“taply中的错误(X=X,INDEX=X,FUN=FUN,…):缺少参数“X”,没有默认值“你确定你的
aes
?我认为有一个括号放错了。是的,有点错误,因为我得到了这个错误:“tapply中的错误(X=X,INDEX=X,FUN=FUN,…):缺少参数“X”,没有默认值”