【问题标题】:Removing NAs from ggplot x-axis in ggplot2从 ggplot2 中的 ggplot x 轴删除 NA
【发布时间】:2020-05-21 08:09:25
【问题描述】:

我想摆脱整个 NA 块(突出显示 here )。 我尝试了 na.ommit 和 na.rm = TRUE 失败。

这是我使用的代码:

library(readxl)
data <- read_excel("Documents/TFB/xlsx_geochimie/solfatara_maj.xlsx")
View(data)
data <- gather(data,FeO:`Fe2O3(T)`,key = "Element",value="Pourcentage")
library(ggplot2)
level_order <- factor(data$Element,levels = c("SiO2","TiO2","Al2O3","Fe2O3","FeO","MgO","CaO","Na2O","K2O"))
ggplot(data=data,mapping=aes(x=level_order,y=data$Pourcentage,colour=data$Ech)+geom_point()+geom_line(group=data$Ech) +scale_y_log10()    

这是我的原始文件 https://drive.google.com/file/d/1bZi7fPWebbpodD1LFScoEcWt5Bs-cqhb/view?usp=sharing

【问题讨论】:

  • 这可能是一个错字,但您是否尝试过只使用一个“m”的 na.omit?
  • 是的,抱歉打错了。我确实尝试过 na.omit。

标签: r ggplot2 na


【解决方案1】:

如果我运行您的代码并查看进入 ggplot 的 data

table(data$Element)
  Al2O3      CaO    Fe2O3 Fe2O3(T)      FeO      K2O      LOI     LOI2      MgO      MnO 
      12       12       12       12       12       12       12       12       12       12 
    Na2O     P2O5     SiO2      SO4     TiO2    Total  Total 2  Total N  Total S 
      12       12       12       12       12       12       12       12       12 

您已将 Total 包含在融化的数据框中。我猜这不是故意的。因此,当您考虑这些因素时,并且这些“Total..”不包括在级别中时,它们变为 NA。

所以我们可以从头开始:

data <- read_excel("solfatara_maj.xlsx")

数据:

structure(list(Ech = c("AGN 1A", "AGN 2A", "AGN 3B", "SOL 4B", 
"SOL 8Ag", "SOL 8Ab", "SOL 16A", "SOL 16B", "SOL 16C", "SOL 22 A", 
"SOL 22D", "SOL 25B"), FeO = c(0.2, 0.8, 1.7, 0.3, 1.7, NA, 0.2, 
NA, 0.1, 0.7, 1.3, 2), `Total S` = c(5.96, 45.3, 0.22, 17.3, 
NA, NA, NA, NA, NA, NA, 2.37, 0.36), SO4 = c(NA, 6.72, NA, 4.08, 
0.06, 0.16, 42.2, 35.2, 37.8, 0.32, 6.57, NA), `Total N` = c(NA, 
NA, NA, NA, NA, NA, NA, NA, NA, 15.2, NA, NA), SiO2 = c(50.2, 
31.05, 56.47, 62.14, 61.36, 75.66, 8.41, 21.74, 17.44, 13.52, 
19.62, 56.35), Al2O3 = c(15.53, 7.7, 17.56, 4.44, 17.75, 10.92, 
31.92, 26.38, 27.66, 0.64, 3.85, 17.28), Fe2O3 = c(0.49, 0.63, 
2.06, NA, 1.76, 0.11, 0.64, 0.88, 1.71, NA, 1.32, 2.67), MnO = c(0.01, 
0.01, 0.13, 0.01, 0.09, 0.01, 0.01, 0.01, 0.01, 0.005, 0.04, 
0.12), MgO = c(0.06, 0.07, 0.88, 0.03, 0.97, 0.05, 0.04, 0.07, 
0.03, 0.02, 1.85, 1.63), CaO = c(0.2, 0.09, 3.34, 0.09, 2.58, 
0.57, 0.2, 0.26, 0.15, 0.06, 35.66, 4.79), Na2O = c(0.15, 0.14, 
3.23, 0.13, 3.18, 2.04, 0.68, 0.68, 0.55, 0.05, 0.45, 3.11), 
    K2O = c(4.39, 1.98, 8, 1.26, 8.59, 5.94, 8.2, 6.97, 8.04, 
    0.2, 0.89, 7.65), TiO2 = c(0.42, 0.27, 0.46, 0.79, 0.55, 
    0.16, 0.09, 0.22, 0.16, 0.222, 0.34, 0.53), P2O5 = c(0.11, 
    0.09, 0.18, 0.08, 0.07, 0.07, 0.85, 0.68, 0.62, NA, 0.14, 
    0.28), LOI = c(27.77, 57.06, 6.13, 29.03, 1.38, 4.92, 42.58, 
    37.58, 38.76, NA, 26.99, 3.92), LOI2 = c(27.79, 57.15, 6.32, 
    29.06, 1.57, 4.93, 42.6, 37.59, 38.77, 0.08, 27.13, 4.15), 
    Total = c(99.52, 99.88, 100.2, 98.25, 99.99, 100.5, 93.81, 
    95.57, 95.23, 15.25, 92.45, 100.3), `Total 2` = c(99.54, 
    99.96, 100.3, 98.28, 100.2, 100.6, 93.83, 95.58, 95.24, 15.33, 
    92.59, 100.6), `Fe2O3(T)` = c(0.71, 1.52, 3.95, 0.27, 3.65, 
    0.22, 0.87, 0.99, 1.82, 0.61, 2.76, 4.9)), row.names = c(NA, 
-12L), class = c("tbl_df", "tbl", "data.frame"))

首先我们像你一样设置绘图级别:

plotlvls = c("SiO2","TiO2","Al2O3","Fe2O3","FeO","MgO","CaO","Na2O","K2O")

然后我们只选择这些列,还有Ech,注意我使用pivot_longer(),因为gather() 应该会被弃用,然后我们也会进行分解:

plotdf = data %>% select(c(plotlvls,"Ech")) %>% 
pivot_longer(-Ech,names_to = "Element",values_to = "Pourcentage") %>%
mutate(Element=factor(Element,levels=toplot))

最后我们绘制,没有 NA:

ggplot(data=plotdf,mapping=aes(x=Element,y=Pourcentage,colour=Ech))+
geom_point()+geom_line(aes(group=Ech)) +scale_y_log10()

【讨论】:

    【解决方案2】:

    1.创建可重现的最小数据

    data <- data.frame(Element = c("SiO2","TiO2","Al2O3","Fe2O3","FeO","MgO","CaO","Na2O","K2O",NA),
                       Pourcentage = 1:10,
                       Ech = c("AGN 1A", "SOL 16"))
    

    2.设置变量'Element'的因子水平

    data$Element <- factor(data$Element,levels = c("SiO2","TiO2","Al2O3","Fe2O3","FeO","MgO","CaO","Na2O","K2O"))
    

    3.删除变量'Element'中包含NA的行

    data <- data[!is.na(data$Element), ]
    

    4.使用 ggplot2 绘制数据(ggplot2 语法使用 NSE(非标准评估),这意味着您不必将变量名称作为字符串传递或使用$ 表示法):

    ggplot(data=data,aes(x=Element,y=Pourcentage,colour=Ech)) +
      geom_point() +
      geom_line(aes(group=Ech)) +
      scale_y_log10()  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-22
      • 2016-05-07
      • 2013-06-17
      • 2016-07-19
      相关资源
      最近更新 更多