R 如何更改ggplot中离散x标度的顺序(x轴上的NA)

R 如何更改ggplot中离散x标度的顺序(x轴上的NA),r,plot,ggplot2,facet,R,Plot,Ggplot2,Facet,我正试图做一个有平面的ggplot2错误条绘图,但失败了。数据很像这样 library(ggplot2) library(dplyr) df0 <- iris %>% group_by(Species) %>% mutate(long_sepal = ifelse(Sepal.Length > mean(Sepal.Length), yes = "long", no = "short")) %>% group_by(Species,

我正试图做一个有平面的ggplot2错误条绘图,但失败了。数据很像这样

library(ggplot2)
library(dplyr)

df0 <- iris %>%
  group_by(Species) %>%
  mutate(long_sepal = ifelse(Sepal.Length > mean(Sepal.Length), 
         yes = "long", no = "short")) %>%
  group_by(Species, long_sepal) %>%
  mutate(petal_rank = order(Petal.Width)) %>%
  filter(petal_rank <= 5) %>%
  mutate(petal_rank = factor(petal_rank))

> df0
# Source: local data frame [30 x 7]
# Groups: Species, long_sepal [6]
# 
#    Sepal.Length Sepal.Width Petal.Length Petal.Width Species long_sepal petal_rank
#           (dbl)       (dbl)        (dbl)       (dbl)  (fctr)      (chr)      (int)
# 1           5.4         3.9          1.7         0.4  setosa       long          1
# 2           4.6         3.4          1.4         0.3  setosa      short          1
# 3           5.0         3.4          1.5         0.2  setosa      short          2
# 4           4.4         2.9          1.4         0.2  setosa      short          3
# 5           4.9         3.1          1.5         0.1  setosa      short          4
# 6           5.4         3.7          1.5         0.2  setosa       long          3
# 7           5.8         4.0          1.2         0.2  setosa       long          4
# 8           5.2         4.1          1.5         0.1  setosa       long          2
# 9           5.5         4.2          1.4         0.2  setosa       long          5
# 10          4.5         2.3          1.3         0.3  setosa      short          5
# ..          ...         ...          ...         ...     ...        ...        ...
绘图代码在x轴上给出了一个
NA
,我想避免

ggplot(data = df0, 
       aes(x = long_sepal, y = Petal.Width, group = factor(petal_rank),
           ymin = Petal.Width-0.05, 
           ymax = Petal.Width+0.05)) + 
  geom_pointrange(position = position_dodge(width = 0.4)) +
  facet_wrap(~ Species, scales = "free") # + scale_x_discrete(limits=c("short", "long"))

。。。或者当我取消注释
scale\u x\u discrete
函数时出现错误消息

Error in if (zero_range(from) || zero_range(to)) { : 
  missing value where TRUE/FALSE needed

是否可以按照离散值的指定顺序进行绘图,并且在没有x轴分类的刻面中没有
NA
标签?

如果将
x
映射到
long_sepal
的分解版本,则可以更改顺序,但这不会消除
NA
;您还需要
缩放x_离散
。但是,您需要设置
中断
,而不是
限制

ggplot(data = df0, 
       aes(x = factor(long_sepal, levels = c('short', 'long')), 
           y = Petal.Width, group = factor(petal_rank),
           ymin = Petal.Width-0.05, 
           ymax = Petal.Width+0.05)) + 
  geom_pointrange(position = position_dodge(width = 0.4)) +
  facet_wrap(~ Species, scales = "free") + 
  scale_x_discrete(breaks = c("short", "long"))

请注意,
factor
方法将主x标签搞乱,但无论如何,您可能希望使用
xlab
或诸如此类的设置。此外,如果有问题,您将丢失
NA
垂直网格线

ggplot(data = df0, 
       aes(x = factor(long_sepal, levels = c('short', 'long')), 
           y = Petal.Width, group = factor(petal_rank),
           ymin = Petal.Width-0.05, 
           ymax = Petal.Width+0.05)) + 
  geom_pointrange(position = position_dodge(width = 0.4)) +
  facet_wrap(~ Species, scales = "free") + 
  scale_x_discrete(breaks = c("short", "long"))