ggplot轴具有重复标签的自定义订单 [英] ggplot axis custom order with duplicate labels

查看:90
本文介绍了ggplot轴具有重复标签的自定义订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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")

如何以与x $ name相同的顺序绘制轴? (是的,k是重复的,我希望将其显示在类似此轴的图中:c k g f o k s v t q)

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)

过去我曾经做过:

x$name <- factor(x$name, levels = x$name[order(x$val)], ordered = T)

由于以下原因,三明治不再起作用:

wich doesn't work any more thanks to: 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:具有重复级别的因素的顺序 他的数据结构完全不同.

This is no duplicate of: ggplot: order of factors with duplicate levels His data structure is completely different.

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

Also, I have tried setting limits in x_scale_discrete. Doesn't work.

推荐答案

尝试一下...

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)

这篇关于ggplot轴具有重复标签的自定义订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆