[R] micro-XRF map visualization using ggplot2


[R] micro-XRF map visualization using ggplot2

I obtained compositional maps of my rock sample (thin-section/chip/slab) using micro-XRF (Bruker). It is the compositional (Fe intensity) map. This map is nice and good for identifying minerals. But I want to change the color scale because better color scales help us to identify the compositional difference easily.


Last time (HERE), I used "plot" in R but 'ggplot' is easier and more beautiful. I will show how to visualize the micro-XRF elemental map using ggplot2 in R!! 

I referred XMaptools which is very useful and user-friendly.



Library

library(dplyr)
library(reshape2)
library(ggplot2)
library(tidyverse)
library(RColorBrewer)
library(viridis)


Read data (raw data of micro-XRF mapping, text file)

test1 <- read.table("PATH HERE/test1.txt", sep =";") 


Data preparation (Rotate)

If I plot using the row data it will be 90 degrees rotated. So I will rotate the data

x <- as.matrix(test1)
(y <- t(x[nrow(x):1,ncol(x):1])[ncol(x):1,])
test <- as.matrix(y)
rownames(test) <- paste("x", 1:nrow(test), sep="")
test <- melt(test)


Visualization micro-XRF map using ggplot2 

p <- ggplot(test, aes(x=Var1, y=Var2, fill = value)) + 
  geom_tile() + 
  theme(axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.title.x = element_blank(),
        axis.title.y = element_blank()) +
  coord_equal() +
  scale_fill_viridis(option = "D")
plot(p)



Visualization micro-XRF map using ggplot2 

Change the color palette

p <- ggplot(test, aes(x=Var1, y=Var2, fill = value)) + 
  geom_tile() + 
  theme(axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.title.x = element_blank(),
        axis.title.y = element_blank()) +
  coord_equal() +
  scale_fill_distiller(palette = "Spectral")
plot(p)


Visualization micro-XRF map using ggplot2 

Change the color palette and scale (manual)

p <- ggplot(test, aes(x=Var1, y=Var2, fill = value)) + 
  geom_tile() + 
  theme(axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.title.x = element_blank(),
        axis.title.y = element_blank()) +
  coord_equal()

p <- p + scale_fill_gradient2(
  low = "white",
  mid = "aquamarine4",
  high = "black",
  midpoint = 2000,
  na.value = "grey50")
plot(p)



Visualization micro-XRF map using ggplot2 

Change the color palette and scale (manual)

p <- ggplot(test, aes(x=Var1, y=Var2, fill = value)) + 
  geom_tile() + 
  theme(axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.title.x = element_blank(),
        axis.title.y = element_blank()) +
  coord_equal()

p <- p + scale_fill_gradient2(
  low = "white",
  mid = "aquamarine4",
  high = "black",
  midpoint = 1500,
  na.value = "grey50",
  limits = c(0, 2500),
  breaks = c(500, 1000, 1500, 2000, 2500),
  labels = c("<500","1000","1500","2000",">2500")
)
plot(p)


Final version (add the title and the scale name)

p <- ggplot(test, aes(x=Var1, y=Var2, fill = value)) + 
  geom_tile() + 
  theme(axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.title.x = element_blank(),
        axis.title.y = element_blank()) +
  coord_equal()

p <- p + scale_fill_gradient2(
  low = "white",
  mid = "aquamarine4",
  high = "black",
  midpoint = 1500,
  na.value = "grey50",
  limits = c(0, 2500),
  breaks = c(500, 1000, 1500, 2000, 2500),
  labels = c("<500","1000","1500","2000",">2500"),
  name = "Fe intensity"
)

p <- p + ggtitle("Peridotite | micro-XRF | R")

plot(p)









Post a Comment

0 Comments