【问题标题】:Editing and formatting box plot in ggplot2-R (removing columns and box width in time series plot)在 ggplot2-R 中编辑和格式化箱形图(删除时间序列图中的列和箱宽)
【发布时间】:2021-10-31 08:01:10
【问题描述】:

如何从我的绘图中删除 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))

【问题讨论】:

  • 请与dput分享您的数据样本
  • @Vinícius Félix 当我将使用 dput 函数获得的数据结构复制并粘贴到堆栈溢出的“添加注释”中时,它显示“174 个字符太长”
  • 试试xlim(NA, 2021),另见限制文档。 ggplot2.tidyverse.org/reference/lims.html
  • 不要将dput 函数添加到添加 cmets 中,直接使用编辑问题按钮将其添加到您的问题中。
  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: r ggplot2 boxplot


【解决方案1】:

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

固定箱线图的宽度

添加一个参数以保留函数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))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-18
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 2021-05-21
    相关资源
    最近更新 更多