Home > OS >  How to make a heatmap with persp function
How to make a heatmap with persp function

Time:06-24

Hello I am trying to draw a 3D production function with different colors.

This production function takes the following form: enter image description here

Where Q represents the production (output), K represents Capital and L represents labor.

I have this:

labour <- seq(1, N, by = 1)
capital <- seq(1, N, by = 1)

cobb_douglas <- function(capital, labour, alpha=0.7, beta=0.25){
  
  capital^(alpha)*labour^(beta)
  
}

production <- outer(capital, labour, cobb_douglas, alpha=0.5, beta=0.5)

persp(labour, capital, production,
      main = "Cobb Douglas Production Function",
      theta = -45, phi = 20,  
      col = "cornflowerblue")

Which display in this graph:

enter image description here

My first question is how can I make the graph have like more colours? Like a heatmap like this one:

enter image description here

My second question is how can I get rid of the arrows in every axis label?

Thanks in advance!

CodePudding user response:

You need the outer product of N-1 columns and rows for your colour vector (e.g.: 2x2 corner points defines 1 block):

persp(labour, capital, production,
      main = "Cobb Douglas Production Function",
      theta = -45, phi = 20,
      col = heat.colors(N-1,rev=TRUE)[replace(production, , cut(production, N))[-N,-N]])

enter image description here

CodePudding user response:

How about an interactive version?

# Sample data
N <- 100
labour <- seq(1, N, by = 1) / N
capital <- seq(1, N, by = 1) / N
cobb_douglas <- function(capital, labour, alpha=0.7, beta=0.25){
    capital^(alpha)*labour^(beta)
}
production <- outer(capital, labour, cobb_douglas, alpha=0.5, beta=0.5)

library(plotly)
plot_ly(x = ~ capital, y = ~ labour, z = ~ production) %>%
    add_surface(colorscale = "Rainbow")

A (static) screenshot:

enter image description here

  • Related