ggplot axis custom order with duplicate labels

use*_*028 2 r ggplot2

set.seed(357)
x <- data.frame(name = sample(letters, 10), val = runif(10), stringsAsFactors = F)
x[c(2,6),"name"] <- c("k","k")
ggplot(x, aes(x = name, y = val)) + theme_bw() + geom_bar(stat = "identity")
Run Code Online (Sandbox Code Playgroud)

How can I plot the axis in the same order as x$name? (Yes, the k is duplicate, I want that to show up in the plot like this axis: c k g f o k s v t q)

In the past I used to do:

x$name <- factor(x$name, levels = x$name[order(x$val)], ordered = T)
Run Code Online (Sandbox Code Playgroud)

由于以下原因,它不再起作用: http://r.789695.n4.nabble.com/factors-with-non-unique-quot-duplicated-quot-levels-have-been-deprecated-since-2009- are-more-depreca-td4721481.html

这与以下内容不重复:ggplot:具有重复级别的因素的顺序 他的数据结构完全不同。

另外,我尝试在 x_scale_discrete 中设置限制。不起作用。

And*_*tar 5

尝试这个...

x$name2 <- 1:nrow(x)
ggplot(x, aes(x = factor(name2), y = val)) + theme_bw() + geom_bar(stat = "identity") + 
             scale_x_discrete(labels=x$name)
Run Code Online (Sandbox Code Playgroud)