I want to visualize geological data. Today's goal is to make this plot using ggplot2 in R
Library
library(ggplot2)
library(tidyverse)
library(ggridges)
library(RColorBrewer)
Load test data (Iris data)data <- iris
View the first six rows of iris data
head(iris)## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
Visualization_testggplot(data, aes(x = Sepal.Length, y = Sepal.Width
)) + geom_point()
I try to visualize geological data. I use "mantle xenolith" data from PetDB.
Load the dataset
data <- read.csv("petdf_mantle_xenolith.csv")
View the first six rows of the datahead(data)
## SAMPLE_ID SAMPLE_NAME IGSN SAMPLE_TYPE LATITUDE LONGITUDE ELEVATION_MIN
## 1 11-01 11-01 Mineral 23.3994 58.1436 NA
## 2 11-10 11-10 Mineral 23.0494 58.6818 NA
## 3 11-33C 11-33C Mineral 23.8822 56.6274 NA
## 4 13-113 13-113 Mineral 25.4489 56.1223 NA
## 5 13-121 13-121 Mineral 25.4489 56.0957 NA
## 6 13-33 13-33 Whole Rock 23.6489 56.9362 NA
## ELEVATION_MAX TECTONIC_SETTING ROCK.NAME REFERENCE
## 1 NA OPHIOLITE LHERZOLITE PRIGENT, 2018[3698]
## 2 NA OPHIOLITE LHERZOLITE PRIGENT, 2018[3698]
## 3 NA OPHIOLITE LHERZOLITE PRIGENT, 2018[3698]
## 4 NA OPHIOLITE LHERZOLITE PRIGENT, 2018[3698]
## 5 NA OPHIOLITE LHERZOLITE PRIGENT, 2018[3698]
## 6 NA OPHIOLITE HARZBURGITE PRIGENT, 2018[3698]
.......
Creating a scatter plot using ggplot() + geom_point() I use x = SiO2 and y = MgOggplot(data, aes(x = SiO2, y = MgO)) + geom_point()
## Warning: Removed 4940 rows containing missing values (geom_point).
Adding color (rock type)
ggplot(data, aes(x = SiO2, y = MgO, color = ROCK.NAME
)) + geom_point()
Changing plot size and shapeggplot(data, aes(x = SiO2, y = MgO, color = ROCK.NAME
)) + geom_point(size = 5, shape = 4)Changing shape (SAMPLE_TYPE)ggplot(data, aes(x = SiO2, y = MgO, color = ROCK.NAME, shape = SAMPLE_TYPE
)) + geom_point(size = 1)Adding label (rock type) on the plotp <- ggplot(data, aes(x = SiO2, y = MgO, color = ROCK.NAME, shape = SAMPLE_TYPE
)) + geom_point(size = 1)
p <- p + geom_text(aes(label=ROCK.NAME))
plot(p)Adding subsetp <- ggplot(data, aes(x = SiO2, y = MgO, color = ROCK.NAME, shape = SAMPLE_TYPE
)) + geom_point(size = 1)
lab <- c("CLINOPYROXENITE", "DUNITE", "ECLOGITE", "GABBRO", "HARZBURGITE",
"LHERZOLITE", "METAPERIDOTITE", "ORTHOPYROXENITE", "PERIDOTITE", "PYROXENITE",
"WEBSTERITE", "WEHRLITE")
p <- p + geom_text(aes(label = ROCK.NAME), color = "black",
data = subset(data, ROCK.NAME %in% lab), check_overlap = TRUE)
plot(p)Change the theme (making background white)p <- ggplot(data, aes(x = SiO2, y = MgO, color = ROCK.NAME, shape = SAMPLE_TYPE
)) + geom_point(size = 1)
p <- p + theme_bw()
plot(p)
Change the color palette version_1p <- ggplot(data, aes(x = SiO2, y = MgO, color = ROCK.NAME, shape = SAMPLE_TYPE
)) + geom_point(size = 1)
p <- p + theme_bw()
p <- p + scale_color_brewer(palette = "RdYlBu")
plot(p)Change the color value (MgO)p <- ggplot(data, aes(x = SiO2, y = MgO, color = MgO, shape = SAMPLE_TYPE
)) + geom_point(size = 1)
p <- p + theme_bw()
p <- p + scale_color_gradient(name = "MGO",
high = "red",
low = "blue")
plot(p)Adding middle colorp <- ggplot(data, aes(x = SiO2, y = MgO, color = MgO
)) + geom_point(size = 1)
p <- p + theme_bw()
p <- p + scale_colour_gradient2(
low = "brown1",
mid = "white",
high = "deepskyblue1",
midpoint = 30,
na.value = "grey50",
)
plot(p)Changing limit and label of colorp <- ggplot(data, aes(x = SiO2, y = MgO, color = MgO
)) + geom_point(size = 1)
p <- p + theme_bw()
p <- p + scale_colour_gradient2(
low = "brown1",
mid = "white",
high = "deepskyblue1",
midpoint = 30,
na.value = "grey50",
limits = c(20, 40),
breaks = c(20, 25, 30, 35, 40),
labels = c("<20","25","30","35",">40")
)
plot(p)
Adding title of the plot and editing the axisp <- ggplot(data, aes(x = SiO2, y = MgO, color = MgO
)) + geom_point(size = 1)
p <- p + theme_bw()
p <- p + scale_x_continuous(name = "SiO2", limits = c(0, 70), breaks = c(0,10,20,30,40,50,60,70))
p <- p + scale_y_continuous(name = "MgO", limits = c(0, 60), breaks = c(0,10,20,30,40,50,60))
p <- p + scale_colour_gradient2(
low = "brown1",
mid = "white",
high = "deepskyblue1",
midpoint = 30,
na.value = "grey50",
limits = c(20, 40),
breaks = c(20, 25, 30, 35, 40),
labels = c("<20","25","30","35",">40")
)
p <- p + ggtitle("PetDB mantle xenolith dataset")
plot(p)
Done!














0 Comments