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)
这与以下内容不重复:ggplot:具有重复级别的因素的顺序 他的数据结构完全不同。
另外,我尝试在 x_scale_discrete 中设置限制。不起作用。
尝试这个...
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)
| 归档时间: |
|
| 查看次数: |
1550 次 |
| 最近记录: |