我有包含一些NA值的数据,我正在尝试绘制如下图:
library(ggplot2)
library(forcats)
library(dplyr)
library(ggpubr)
df<-data.frame(Y = rnorm(20, -6, 1),
X = sample(c("yes", "no", NA), 20, replace = TRUE))
dfplot<- df %>% mutate(X=fct_relevel(X, "yes"))%>%
ggplot(.,
aes(x=X, y=Y, fill=X))+
geom_boxplot(size=1, width = 0.2, show.legend = F, outlier.shape = NA,
position=position_nudge(x=0.3))+
geom_jitter(show.legend = T, shape=21, width=0.2, size=2)+
geom_crossbar(data=df %>% group_by(X) %>% summarise(mean=mean(Y), .groups="keep"),
aes(x=X, ymin=mean, ymax=mean, y=mean), width = 0.2, show.legend = F)+
labs(x="",
y="%")
dfplot

但是,当我尝试仅绘制"yes“和"no”变量时,使用filter(X!= "NA“)删除”NA“,我无法将它们重新排列到第一列为"yes”的正确顺序。如果我使用drop_na("X")或filter(!is.na(X))而不是filter(X!="NA"),也会发生同样的情况
dfplot<- df %>% filter(X!="NA") %>% mutate(X=fct_relevel(X, "yes"))%>%
ggplot(.,
aes(x=X, y=Y, fill=X))+
geom_boxplot(size=1, width = 0.2, show.legend = F, outlier.shape = NA,
position=position_nudge(x=0.3))+
geom_jitter(show.legend = T, shape=21, width=0.2, size=2)+
geom_crossbar(data=df %>% group_by(X) %>% summarise(mean=mean(Y), .groups="keep"),
aes(x=X,ymin=mean, ymax=mean, y=mean), width = 0.2, show.legend = F)+
labs(x="",
y="%")
dfplot

发布于 2021-08-22 11:12:34
我认为原因是因为您在'geom_crossbar‘中提供了相同的数据,但没有指定删除'NA’值。
尝试在代码块的开头使用"set.seed“,以使其完全可重现。
下面应该会在正确的级别上生成一个带有'yes‘和'no’的图。
library(ggplot2)
library(forcats)
library(dplyr)
library(ggpubr)
set.seed(123456)
df <- data.frame(Y = rnorm(20, -6, 1),
X = sample(c("yes", "no", NA), 20, replace = TRUE))
dfplot <- df %>% filter(!is.na(X)) %>% mutate(X=fct_relevel(X, 'yes')) %>%
ggplot(.,
aes(x=X, y=Y, fill=X))+
geom_boxplot(size=1, width = 0.2, show.legend = F, outlier.shape = NA,
position=position_nudge(x=0.3))+
geom_jitter(show.legend = T, shape=21, width=0.2, size=2)+
geom_crossbar(data=df %>% filter(!is.na(X)) %>% group_by(X) %>% summarise(mean=mean(Y), .groups="keep"),
aes(x=X,ymin=mean, ymax=mean, y=mean), width = 0.2, show.legend = F)+
labs(x="",
y="%")
dfplot发布于 2021-08-22 10:56:04
尝试在scale_fill_discrete中使用na.translate参数,并将其设置为TRUE。然后,NA值将映射到您的填充颜色。关于如何做到这一点,Here是一个最小的例子。
df<-data.frame(Y = rnorm(20, -6, 1),
X = sample(c("yes", "no", NA), 20, replace = TRUE))
dfplot<- df %>%
mutate(X=fct_relevel(X, "yes", "no"))%>%
ggplot(.,
aes(x=X, y=Y, fill=X))+
geom_boxplot(size=1, width = 0.2, show.legend = F, outlier.shape = NA,
position=position_nudge(x=0.3))+
geom_jitter(show.legend = T, shape=21, width=0.2, size=2)+
geom_crossbar(data=df %>% group_by(X) %>% summarise(mean=mean(Y), .groups="keep"),
aes(x=X, ymin=mean, ymax=mean, y=mean), width = 0.2, show.legend = F)+
labs(x="",
y="%") +
scale_fill_discrete(na.translate = TRUE)
dfplothttps://stackoverflow.com/questions/68880438
复制相似问题