这是我的简单资产数据集:
ID Type Currency Value
a Bond GBP 10
b Bond EUR 20
c Stock GBP 3
d Stock GBP 60
e Bond GBP 8
f Bond USD 39
g Stock USD 1
以下是代码:
assets <- read_excel("C:/R/SampleData.xlsx")
g <- ggplot(assets, aes(Currency, Value))
g + geom_col()
这是我得到的图:
我想创建相同的图,但是希望按值(降序)对条形图进行排序。如何做到这一点?我尝试了以下代码:
assets$Currency <- factor(assets$Currency, levels = assets$Currency[order(assets$Value)])
但是我收到以下警告信息,图表仍未正确排序:
Warning message:
In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else paste0(labels, :
duplicated levels in factors are deprecated
谢谢!
assets$Currency <- reorder(assets$Currency, assets$Value, sum)
- MrFlick