在 ggplot2-R 中编辑和格式化箱形图(删除时间序列图中的列和箱宽)

Editing and formatting box plot in ggplot2-R (removing columns and box width in time series plot)

如何从我的图中删除 NA 列(红色圆圈内)?。另外,如何将 2021 年(蓝色圆圈内)的箱线图的宽度固定为与往年(2013 年至 2019 年)的尺寸相似?我的数据结构有 577 行和 27 列。尽管对于某些参数,我有“NA”值。但是,对于正磷酸盐(附图),我没有任何 NA 值。谢谢你。 R代码如下所示

p<-ggplot(df2, aes(x=Year, y=Ortho.P..µM,fill=factor(Season2))) +
  geom_boxplot()

p + labs(title = "clark", x = "Year", y = "Orthophosphate(µM), 
  Surface")+scale_fill_discrete(name = "Season")+
  theme(plot.title = element_text(hjust = 0.5))+
  theme(text=element_text(family="Times New Roman", face="bold", size=14))

好的,因为我没有数据,我想这应该行得通

固定箱形图的宽度

添加一个参数以保留函数内的宽度geom_boxplot

geom_boxplot(position = position_dodge(preserve = "single"))

删除 NA 列

ggplot 过滤 Year

中的 NA 之前
filter(!is.na(Year))

最终解决方案

df2 %>% 
  filter(!is.na(Year)) %>% 
  ggplot(aes(x=Year, y=Ortho.P..µM,fill=factor(Season2))) +
  geom_boxplot(position = position_dodge(preserve = "single"))+
  labs(
    title = "clark",
    x = "Year",
    y = "Orthophosphate(µM), Surface")+
  scale_fill_discrete(name = "Season")+
  theme(plot.title = element_text(hjust = 0.5))+
  theme(text=element_text(family="Times New Roman", face="bold", size=14))