rplot

Why is image() using different colors than the ones I specified?


I want to plot the facies of a well log in R. My data has a column called LFC with integers from 0 to 4 where:

I already did it in Python but I need it in R, my code and result is:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.colors as colors
logs = pd.read_csv('data/proyectoFinal/well_log.csv')
ccc = ['#B3B3B3','blue','green','red','#996633',]
cmap_facies = colors.ListedColormap(ccc[0:len(ccc)], 'indexed')
cluster=np.repeat(np.expand_dims(logs['LFC'].values,1), 100, 1)
im=plt.imshow(cluster, interpolation='none', aspect='auto',cmap=cmap_facies,vmin=0,vmax=4)
cbar=plt.colorbar(im)
cbar.set_label((12*' ').join(['undef', 'brine', 'oil', 'gas', 'shale']))
cbar.set_ticks(range(0,1)); cbar.set_ticklabels('')

lithofacies column in Python

Meanwhile in R:

logs <- read.csv('data/proyectoFinal/well_log.csv')
ccc <- c('#B3B3B3', 'blue', 'green', 'red', '#996633')
cluster <- matrix(rep(logs$LFC, each = 100), nrow = 100)
colores <- as.vector(ccc)
image(t(cluster), col = colores, axes = FALSE, xlab = "", ylab = "")
axis(2, at = seq(1, nrow(cluster), length.out = length(ccc)), labels = c('undef', 'brine', 'oil', 'gas', 'shale'))
axis(1, at = seq(1, ncol(cluster), length.out = 10), labels = FALSE)
box()

lithofacies column in R


Solution

  • Set the z-limits:

    image(t(cluster), col = colores, axes = FALSE, xlab = "", ylab = "", zlim=c(0, 4))
    

    From the documentation:

    zlim
    the minimum and maximum z values for which colors should be plotted, defaulting to the range of the finite values of z. Each of the given colors will be used to color an equispaced interval of this range. The midpoints of the intervals cover the range, so that values just outside the range will be plotted.

    What this means is that by default, image() spreads out your z values over the full range of col as equally as it can. Since your data doesn't have any 0s, it assigns 1 to the 1st color, 2 to "2.33nd" color, 3 to the "3.67th" color, and 4 to the 5th color in order to spread the values over the full color range you gave it. Setting zlim overrides that.

    Also, if you want it plotted veritcally instead of horizontally, just remove the transpose (t()), and use rev(log$LFC) in place of log$LFC so it doesn't come out upside-down.