简体   繁体   中英

How do I plot the response of two variables in ggplot2 R

I have two variables my x is the size of an island (log_p) and my y is island isolation (log_is). Both of these variables influence my species richness (chao). I want to create a plot that shows how these two factors affect richness using a dark colour to show high species richness and light colour for low.

I've been trying to do stat_density2d plots but it doesn't work.

Also tried:

ggplot(cont, aes(x=log_p, y=log_is, fill=chao)) + geom_tile(aes=fill)

Any ideas?

My data

structure(list(log_is = c(2.784582, 3.965976, 3.160123, 3.965856, 
2.171726, 3.090473, 2.738495, 2.852071, 3.826299, 2.582677, 2.911589, 
2.879027, 2.735663, 2.875952, 2.567344, 2.840363, 2.710743, 3.913372, 
3.579155, 3.871543, 2.911456, 2.616497, 3.14677, 3.159763, 3.283799, 
3.84293, 3.896117, 2.847517, 2.830306, 2.928196, 3.822751, 3.902923, 
4.025753, 3.076946, 3.048442, 4.17537, 3.112166, 3.123662, 3.124886, 
3.044046, 3.076488, 3.540467, 3.098806, 3.070673, 4.025168, 2.871053, 
2.950545, 2.410777, 2.853765, 3.245895, 3.8573, 2.883894, 2.394574, 
3.931298, 3.919264, 2.969621, 3.895399, 4.028566, 4.012367, 3.830491, 
3.691208, 2.793972, 3.999179, 2.824555, 4.028418, 2.807745, 3.09482, 
3.905342, 3.959665, 3.991713, 2.668274, 1.529045, 2.733639, 4.081603, 
2.799706, 3.905871, 3.892379, 3.815208, 2.724489, 3.894947, 3.885924, 
3.872594, 4.024221, 4.003464, 3.82926, 4.010739), log_p = c(2.010256, 
1.641483, 2.438906, 1.760602, 2.095772, 1.560649, 1.61889, 1.336108, 
2.039922, 2.110998, 2.735788, 1.699439, 1.441849, 2.576832, 2.092638, 
2.522571, 1.979817, 1.333593, 1.965031, 2.257994, 1.70236, 3.344115, 
2.606779, 2.089825, 1.744152, 2.159306, 1.544484, 1.556532, 2.05153, 
2.424077, 2.167386, 1.703555, 2.111546, 1.345542, 1.708146, 1.470107, 
1.772433, 2.813496, 1.910502, 1.471081, 1.714327, 1.559928, 2.609404, 
1.345577, 2.178536, 1.851249, 3.26387, 2.748153, 1.160588, 3.180089, 
1.820134, 2.345077, 2.86294, 1.32498, 1.340593, 1.551324, 1.765069, 
1.635757, 1.897482, 2.013189, 2.448145, 1.828805, 1.464607, 1.862804, 
2.362365, 2.160348, 1.563706, 2.087313, 1.638022, 1.163253, 1.867932, 
2.558451, 2.216077, 2.350735, 2.089179, 2.117508, 2.37918, 1.851642, 
2.490327, 1.560837, 2.037264, 2.162884, 2.199939, 1.94566, 1.819947, 
2.691155), chao = c(433.32051, 116.72727, 162, 145.32692, 287.95161, 
94.44444, 171, 251.92, 163.28571, 259.35714, 262.0125, 202.26667, 
211.32, 266.9, 349.43902, 333.05714, 311.10227, 56.08333, 50.16667, 
109.44048, 203.21429, 313.52128, 285.44681, 93.55556, 44.125, 
50.51562, 88.28125, 147, 31.08333, 215.37931, 95.55882, 120, 
271.7234, 59.53846, 75.22222, 125.32143, 70.16667, 109.57143, 
99.88462, 56.78571, 37.45, 48.66667, 71, 53.5, 142.06522, 189.09091, 
243.22368, 18.6, 213.28947, 229.91379, 117.34783, 121.32258, 
35.57143, 118.83333, 35, 145.52083, 78.75, 175.08696, 137.5, 
93.4, 124.375, 165.7, 179.52083, 267.5625, 285, 39.125, 122.15385, 
117.15, 88.25, 213.94118, 395.05263, 50.33333, 222.21429, 133.01316, 
180.52083, 89.11842, 67.04167, 95.44444, 70.64286, 84.125, 47, 
199.98276, 174.02083, 87.25, 113.47059, 124.88462)), .Names = c("log_is", 
"log_p", "chao"), row.names = c(NA, -86L), class = "data.frame")

As an addition to Ben Bolker answer try this:

    ggplot(cont, aes(x=log_p, y=log_is, colour=chao)) +
  geom_point(size=5) +
  scale_colour_gradient2(name = "Species Richness", low="white", high="red") +

  labs(title = "Your title")+ 
  labs(x = "size of an island ", y = "island isolation") +

  theme(plot.title = element_text(size = rel(2),face="bold"),

        axis.text=element_text(size=12),
        axis.title=element_text(size=16,face="bold"))

With some nice axis, titles, legend, colour gradient and so on.

Good luck with it

This code gave me the results i was looking for. Now just trying to increase the resolution of the output.

library(akima)
library(reshape2)

d1 <- with(cont, interp(x = log_p, y = log_is, z = chao))

# melt the z matrix in d1 to long format for ggplot
d2 <- melt(d1$z, na.rm = TRUE)
names(d2) <- c("x", "y", "chao")

# add log_p and log_is from d1 using the corresponding index in d2
d2$log_p <- d1$x[d2$x]
d2$log_is <- d1$y[d2$y]

# plot
ggplot(data = d2, aes(x = log_p, y = log_is, fill = chao, z = chao)) +
  geom_tile() +theme_bw()+
  stat_contour(bins=1)+scale_fill_gradient(low="white", high="black")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM