Построение другой оси X с использованием граней - PullRequest
1 голос
/ 20 февраля 2020

Я строю диаграммы-пауки для геохимического анализа.

Мне удалось построить отдельные серии элементов отдельно (основные элементы и редкоземельные элементы), но я хотел бы построить их вместе, используя facet_grid. Проблема, которую я получаю, состоит в том, что ось X является общей. Я хотел бы иметь две отдельные оси X, как показано в сообщении imgur: https://imgur.com/a/7YnPio1

Я написал прокомментированные коды о том, чего я достиг:

library(readxl)
library(tidyr)
library(dplyr)
library(ggplot2)


data <- read_excel("Documents/TFB/xlsx_geochimie/solfa_total_tout_ppm.xlsx", 
                   col_types = c("text", "numeric", "numeric", 
                                 "numeric", "numeric", "numeric", 
                                 "numeric", "numeric", "numeric", 
                                 "numeric", "numeric", "numeric", "numeric", 
                                 "numeric", "numeric", "numeric", 
                                 "numeric", "numeric", "numeric", "numeric", 
                                 "numeric", "numeric", "numeric", "numeric", 
                                 "numeric", "numeric", "numeric", "numeric", "numeric", 
                                 "numeric", "numeric", "numeric", "numeric", "numeric", 
                                 "numeric", "numeric", "numeric", 
                                 "numeric", "numeric", "numeric", "numeric", 
                                 "numeric", "numeric", "numeric", "numeric", 
                                 "numeric", "numeric", "numeric", 
                                 "numeric", "numeric", "numeric", 
                                 "numeric", "numeric", "numeric", "numeric", 
                                 "numeric", "numeric", "numeric", "numeric", 
                                 "numeric", "numeric", "numeric", "numeric", 
                                 "numeric", "numeric", "numeric", "numeric"))

### Vectors containing different geochemical series

vec_maj = c("SiO2","TiO2","Al2O3","FeO","MgO","CaO","Na2O","K2O")
vec_TR = c("La","Ce","Pr","Nd","Sm","Eu","Gd","Tb","Dy","Ho","Er","Tm","Yb","Lu")
vec_tout <- as.character(c(vec_maj,vec_TR))
data.mod <- data[vec_tout]
data.mod$Ech <- data$Ech
### Wide format to long format
data.lf = data %>% select(c(vec_tout,"Ech")) %>%
  pivot_longer(-Ech,names_to="Element",values_to="Pourcentage") %>%
  mutate(Element=factor(Element,levels=unique(vec_tout)))
### Plotting the series separately

data.maj <- subset(data.lf,data.lf$Element %in% vec_maj)
View(data.maj)
data.TR <- subset(data.lf,data.lf$Element %in% vec_TR)

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

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

# Plotting the series together, x-axis scales does not split :-( 
data.lf$Type <- ifelse(data.lf$Element %in% vec_maj,"Major","REE")
ggplot(data=data.lf,mapping=aes(x=Element,y=Pourcentage,colour=Ech))+
  geom_point()+geom_line(aes(group=Ech))+scale_y_log10()+facet_grid(Type~.,scales="free")

Вы можете скачать мой набор данных здесь: Google Drive

1 Ответ

2 голосов
/ 20 февраля 2020

Вместо facet_grid, вы можете использовать facet_wrap?

ggplot(data.lf, mapping = aes(x = Element, y = Pourcentage, colour = Ech)) +
  geom_point() +
  geom_line(aes(group = Ech)) +
  scale_y_log10() +
  facet_wrap(Type ~ ., ncol = 1, scales = "free")
# Warning: Removed 8 rows containing missing values (geom_point).

facet wrap

...