2015-05-03 69 views
0

我有兩個變量,我的x是島的大小(log_p),我的y是孤島(log_is)。這兩個變量都會影響我的物種豐富程度(chao)。我想創建一個情節,顯示這兩個因素如何影響豐富性,使用深色顯示高物種豐富度和低顏色。如何繪製ggplot2中兩個變量的響應R

我一直在試圖做stat_density2d圖,但它不起作用。

也試過:

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

任何想法?

我的數據

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., 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") 
+5

您能否提供可複製的數據和您的代碼? – jazzurro

+2

[關於如何使重現性的信息](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Jaap

+0

我認爲[this thread](http:///stackoverflow.com/questions/22894609/trouble-displaying-contours-in-r-and-ggplot-with-basic-dataset)可以幫助你實現你的目標。 – essicolo

回答

1

作爲除本Bolker回答試試這個:

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")) 

有了一些不錯的軸,標題,傳奇,顏色漸變等。

祝你好運

0

此代碼給了我想要的結果。現在只是試圖提高輸出的分辨率。

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") 
+0

您可以通過'xo'和'yo'參數將分辨率提高到'interp()'(請參閱'interp'),但是您可能不想提供數據的分辨率:如果'g0'是您的原始圖片,試試'g0 + geom_point(data = cont,aes(color = chao))' –

相關問題