【发布时间】:2020-08-05 22:02:23
【问题描述】:
情况如下:
我有很多名字和很多对应的代码。
所有不同的名称都有唯一的代码,但并非所有不同的代码都有唯一的名称。
这在绘制数据时产生了一个问题,因为我在绘制时需要group_by(code) 和reorder(name,code),但是代码是无意义的,我想显示名称。由于某些代码共享名称,这会产生一些问题。
下面举例说明:
library(tidyverse)
set.seed(1)
# example df
df <- tibble("name" = c('apple','apple','pear','pear','pear',
'orange','banana','peach','pie','soda',
'pie','tie','beer','picnic','cigar'),
"code" = seq(1,15),
"value" = round(runif(15,0,100)))
df %>%
ggplot(aes(x=reorder(name,value)))+
geom_bar(aes(y=value),
stat='identity')+
coord_flip()+
ggtitle("The axis labels I want, but the order I don't")
df %>%
ggplot(aes(x=reorder(code,value)))+
geom_bar(aes(y=value),
stat='identity')+
coord_flip()+
ggtitle("The order I want, but the axis labels I don't")
不太清楚如何让 ggplot 保持第二个图的显示和顺序,同时能够用第一个图的名称替换轴标签。
【问题讨论】: