Jessica
Jessica

Reputation: 47

Plot scatter plot on top of heatmap

I would like to plot a scatter plot onto a heatmap.I made each type of plot with ggplot.

Scatter plot:

lambir_t <- ggplot(l_tree, aes(x=xcoord, 
[![enter image description here][1]][1]y=ycoord))+geom_point(col='red')+xlim(c(0, 1040))+ylim(c(0, 500))

enter image description here

Heat map:

lambir_p <- ggplot(dfo, aes(x,y,
fillz))+geom_tile(color='white')+scale_fill_gradient(low='white', 
high='blue')+labs(x='x', y='y', title='Lambir Hills', 
fill='Phosphorus')

enter image description here

Now I'd like to lay the scatter plot on top of the heat map so that I can see how those points vary with phosphorus concentration. Any ideas on how I can do this?

EDIT: enter image description here

The solution from @phalteman worked. This is what I got. I'm having trouble adjusting the size of the points (it looks the same when set to 1 or 100), but otherwise this is exactly what I was looking for.

Upvotes: 0

Views: 2681

Answers (2)

sbmrtnz
sbmrtnz

Reputation: 1

I found that using the same dataset, but making it explicit in each layer makes it easy to plot points on top of the tiles.

Colours work as intended. Sizes worked better when set a different value to all of them, oddly

ggplot(data = mydata, aes(x = baseofheatmap, y = heightofheatmap)) + 
  geom_tile(data = mydata, 
            aes(fill = valuetoheatmap)) +
  geom_point(data = mydata, 
             aes(x = valuetopoint, 
                 y = heightofheatmap, 
                 size = valuetopoint, 
                 color = "red"))

Upvotes: 0

phalteman
phalteman

Reputation: 3532

You can add multiple layers to your plot using different data sets. Add the points as you normally would but specify the data argument. Without data it's hard to know, but something like this will probably do the trick for you:

ggplot(dfo, aes(x,y,fillz)) + 
  geom_tile(color='white') + 
  scale_fill_gradient(low='white', high='blue') + 
  labs(x='x', y='y', title='Lambir Hills', fill='Phosphorus') +
  geom_point(data=l_tree, aes(x=xcoord, [![enter image description here][1]][1], y=ycoord)), col='red')

Upvotes: 2

Related Questions