ggplot有三个因素和双误差条

时间:2013-11-21 01:17:12

标签: r ggplot2 r-factor

我需要一个xy图,用三个因子绘制x和y的平均值和误差线。这三个因素是年(2004-2012),物种(FW,HB)和地区(科迪亚克,舒马金群岛); xmean =平均d13C和ymean =平均15N。

我可以使用下面的代码合理地接近,但是缺少一个因素,并且它不是非常美观。我还收到了超出形状调色板的警告信息。

library(ggplot2)
library(plyr)

GAP_Whales<-structure(list(Species = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 1L, 1L, 2L), .Label = c("FW", "HB"), class = "factor"), Year = c(2007L, 
2007L, 2007L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 
2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 
2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 
2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 
2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 
2004L, 2004L, 2004L, 2004L, 2005L, 2005L, 2005L, 2005L, 2005L, 
2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 
2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 
2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 
2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 
2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 
2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 
2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 
2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2006L, 2006L, 2006L, 
2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 
2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 
2006L, 2006L, 2006L, 2006L, 2007L, 2005L, 2005L, 2005L, 2005L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 
2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2008L, 2008L, 2007L, 
2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 
2007L, 2007L, 2007L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 
2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 
2008L, 2008L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 
2009L, 2009L, 2005L, 2005L, 2007L, 2007L, 2007L, 2008L, 2008L, 
2008L, 2008L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2007L
), Region = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L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, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("Kodiak", "Shumagin Is."), class = "factor"), d13C = c(-17.741, 
-17.841, -17.382, -16.955, -17.504, -17.42814286, -15.89, -16.421, 
-17.21328571, -17.90142857, -17.654, -19.225, -19.03361128, -18.29057143, 
-17.28, -16.897, -18.18585714, -18, -17.619, -17.47014286, -18.382, 
-16.807, -18.55242857, -18.527, -17.72557143, -17.06471429, -18.02757143, 
-17.599, -17.57614286, -17.36385714, -17.19728571, -18.09871429, 
-17.3, -15.928, -17.26071429, -17.85271429, -17.39342857, -16.98, 
-16.847, -17.84728571, -16.673, -17.174, -16.277, -17.965, -17.60985714, 
-16.6, -17.47885714, -17.46, -17.73342857, -17.028, -18.332, 
-18.548, -18.22, -18.035, -17.138, -17.387, -18.314, -18.27, 
-17.904, -18.497, -18.264, -18.593, -18.264, -18.008, -17.687, 
-18.227, -17.849, -17.713, -18.017, -18.723, -18.793, -17.914, 
-18.31, -18.116, -18.65, -17.587, -17.985, -18.793, -17.833, 
-17.613, -17.942, -17.86, -17.749, -17.222, -17.286, -17.842, 
-18.042, -17.912, -17.858, -18.916, -18.443, -18.638, -18.085, 
-17.974, -17.997, -18.387, -18.129, -17.85, -18.699, -18.754, 
-18.022, -18.636, -18.197, -18.645, -18.149, -18.157, -18.695, 
-18.413, -17.978, -18.447, -17.854, -19.433, -18.251, -17.527, 
-17.732, -18.42, -18.089, -17.498, -18.805, -17.677, -17.721, 
-18.194, -18.063, -16.987, -18.34342857, -18.46185714, -17.56328571, 
-17.84671429, -17.93814286, -18.10157143, -17.786, -17.78442857, 
-17.38885714, -16.61228571, -15.97971429, -18.73614286, -18.26371429, 
-18.98442857, -17.47014286, -18.12185714, -17.89457143, -18.17728571, 
-18.234, -18.83871429, -18.82342857, -18.34314286, -18.43685714, 
-18.66757143, -18.6295, -17.553, -17.72555609, -17.42890918, 
-18.0937904, -17.3725821, -21.242, -20.107, -19.11, -17.771, 
-18.125, -18.577, -17.781, -19.292, -16.776, -20.212, -20.539, 
-17.972, -17.986, -18.634, -17.352, -17.409, -17.93, -17.458, 
-17.53, -17.321, -17.11, -17.929, -17.244, -17.858, -17.251, 
-18.06, -18.22, -18.142, -19.314, -18.412, -17.941, -17.909, 
-18.114, -18.783, -18.181, -17.754, -18.484, -17.463, -18.379, 
-18.19, -18.227, -17.414, -17.824, -17.436, -17.432, -17.171, 
-17.483, -17.64, -17.639, -18.572, -18.545, -18.007, -18.033, 
-18.102, -18.301, -17.731, -17.565, -17.68, -17.751, -18.134, 
-18.409, -18.336, -18.888, -18.477, -18.25, -18.121, -18.082, 
-17.914, -19.337, -19.228, -18.455, -18.657, -18.081, -18.23, 
-18.777, -18.935, -18.823, -19.49, -18.383, -18.73, -18.152, 
-18.582, -18.653, -18.407, -18.024, -18.994, -17.831, -17.947, 
-17.57, -18.142, -17.691, -17.869, -18.513, -18.522, -17.923, 
-18.353, -18.278, -17.664, -17.995, -17.786, -18.161, -18.119, 
-18.125, -17.098, -17.576, -18.099, -18.713, -17.4, -17.622, 
-17.532, -18.007, -18.146, -16.692, -18.678, -19.18, -18.522, 
-18.572, -18.476, -19.144, -17.709, -17.742, -18.606, -18.267, 
-18.543, -18.301, -19.117, -18.75, -19.394, -19.219, -18.179, 
-18.681, -18.835, -18.456, -18.323, -18.148, -18.263, -17.965, 
-19.337, -18.301, -19.046, -18.768, -18.017, -17.928, -17.314
), d15N = c(14.166, 14.279, 14.092, 13.464, 13.4, 13.179, 12.895, 
13.537, 13.857, 13.775, 14.147, 12.017, 12.531, 12.329, 13.414, 
13.777, 12.639, 13.135, 13.833, 13.68, 12.317, 12.237, 11.707, 
12.318, 13.574, 14.77, 12.722, 13.772, 13.658, 13.804, 14.07, 
15.182, 14.143, 13.54, 12.932, 13.77, 14.332, 12.642, 13.166, 
12.412, 12.452, 14.09971429, 13.14, 13.643, 13.393, 13.759, 13.791, 
13.244, 12.997, 13.86, 15.53828571, 14.42107143, 14.88228571, 
13.32828571, 14.17421429, 12.94985714, 13.21614286, 11.18814286, 
12.53371429, 12.67442857, 13.50585714, 12.64092857, 12.83257143, 
12.03907143, 12.54642857, 13.70371429, 13.18142857, 14.76085714, 
12.74385714, 13.7225, 11.76364286, 13.66457143, 12.65378571, 
12.50114286, 14.27671429, 14.10342857, 14.3445, 11.72657143, 
12.90221429, 14.71314286, 14.71907143, 14.04371429, 13.75092857, 
13.74578571, 14.94164286, 13.07035714, 13.07685714, 12.8775, 
13.86664286, 12.87185714, 13.75214286, 13.20285714, 12.46021429, 
13.13914286, 13.82028571, 12.52585714, 13.4975, 12.88071429, 
12.48042857, 14.29857143, 13.56214286, 13.41, 13.52985714, 13.55592857, 
12.80007143, 12.91257143, 13.37457143, 13.60371429, 13.88671429, 
13.44635714, 14.18214286, 10.09042857, 12.11571429, 13.00771429, 
15.45157143, 13.33135714, 14.58378571, 11.78642857, 12.47628571, 
14.46642857, 12.37064286, 13.44335714, 12.39628571, 14.08, 14.0505, 
14.34, 14.0145, 13.926, 13.2355, 13.111, 12.3725, 13.888, 13.1075, 
14.015, 14.9595, 12.857, 13.277, 12.457, 12.137, 13.124, 13.299, 
12.811, 12.231, 11.829, 12.263, 13.036, 13.331, 12.76, 12.262, 
14.026, 13.452, 13.769, 13.221, 13.059, 12.754, 12.637, 13.025, 
15.123, 14.006, 12.605, 12.636, 14.229, 15.527, 11.583, 13.004, 
12.851, 12.921, 12.273, 13.922, 13.429, 12.494, 13.803, 13.55, 
13.387, 14.887, 14.248, 14.673, 14.603, 12.879, 12.4, 13.676, 
13.648, 13.067, 13.353, 11.703, 14.118, 12.78, 12.293, 12.68, 
13.494, 13.309, 13.838, 12.688, 14.418, 14.357, 14.587, 14.714, 
14.435, 13.418, 13.013, 12.631, 12.704, 13.091, 12.953, 12.751, 
12.409, 12.921, 12.216, 12.594, 12.698, 14.891, 14.692, 13.187, 
13.451, 13.023, 11.957, 12.401, 12.527, 13.47, 11.771, 11.848, 
12.399, 12.502, 12.678, 12.768, 12.716, 12.671, 12.61, 13.132, 
12.999, 13.251, 11.048, 14.384, 12.688, 13.196, 12.875, 13.495, 
12.895, 12.992, 12.888, 13.044, 14.195, 13.643, 13.042, 13.15, 
13.437, 13.835, 14.884, 13.136, 14.384, 13.927, 14.914, 12.978, 
12.841, 13.793, 14.312, 14.219, 14.36, 13.529, 11.837, 13.166, 
13.103, 12.798, 13.529, 12.813, 9.574, 13.859, 12.548, 13.405, 
12.6, 12.373, 12.964, 12.896, 13.067, 13.896, 14.533, 14.024, 
13.042, 13.213, 13.857, 12.857, 12.393, 11.841, 13.702, 13.634, 
14.391, 13.719, 13.181, 13.566, 13.314, 13.457, 12.871, 12.383, 
13.62, 13.753, 13.388, 12.856, 14.408)), .Names = c("Species", 
"Year", "Region", "d13C", "d15N"), class = "data.frame", row.names = c(NA, 
-298L))

means <- ddply(GAP_Whales, .(Species, Year, Region), function(x) c(xmean=mean(x$d13C), xsd=sd(x$d13C), ymean=mean(x$d15N), ysd=sd(x$d15N)))
Species<-as.factor(means$Species)
Region<-as.factor(means$Region)
Year<-as.factor(means$Year)
p<-ggplot(means, aes(x=means$xmean, y=means$ymean))
p<-p+geom_point(aes(shape=factor(Year), color=factor(Region))) 
p<-p + geom_errorbar(aes(ymin=ymean-ysd, ymax=ymean+ysd), width=.1)+
geom_errorbarh(aes(xmin=xmean-xsd, xmax=xmean+xsd), width=.1)
p

0 个答案:

没有答案