在ggplot

时间:2016-02-23 12:10:40

标签: r ggplot2

我的数据通过dput包含在下面,如下所示:

> head(dat)
     General      Covariate       Beta     SE Season
1  Intercept      Intercept -4.0673077 0.0499 Summer
2 Reggedness SlopeVar_30Log  0.1835911 0.0130 Summer
3  Elevation         Ele_30  0.6194081 0.0186 Summer
4      Slope      Slope_500  2.3549884 0.0527 Summer
5    SlopeSq    Slope_500Sq -0.6729443 0.0326 Summer
6     CanCov    CanCov_1000 -0.3879034 0.0234 Summer

我有Summer的8个估算值和Winter的9个估算值。

根据这些数据,我可以制作下图。在这种情况下,我将General作为x轴,General内的每个值都绘制在一条线上并躲闪。但是,我想用General名称标记Covariate的多个值。 General的每个级别都有两个Covariate

级别
ggplot(dat [dat $General != "Intercept",], aes(y = Beta, x = General, color = Season)) + 
  geom_point(position=position_dodge(0.6), size = 5)+
  geom_errorbar(aes(ymin=Beta-SE, ymax=Beta+SE),width = 0.6, size = 1.2 , position=position_dodge(0.6))

enter image description here

如果我只是将x轴更改为Covariate(如下所示),那么每个级别都在一个单独的行上,除非级别与Ele_30相同。

ggplot(dat [dat $General != "Intercept",], aes(y = Beta, x = Covariate, color = Season)) + 
  geom_point(position=position_dodge(0.6), size = 3)+
  geom_errorbar(aes(ymin=Beta-SE, ymax=Beta+SE),width = 0.6, size = 1.2 , position=position_dodge(0.6))+
  theme(axis.text.x=element_text(angle=30, hjust=1))

enter image description here

是否可以在同一垂直线上绘制每个级别的General(如上图中的第一个图),但是根据Covariate标记每个点(如下图所示)?换句话说,我希望绘制的点如图1所示,并且每个避让点下的标签对应Covariate

感谢您的帮助。

library(ggplot2)
dat <- structure(list(General = structure(c(3L, 5L, 2L, 6L, 7L, 1L, 
4L, 8L, 3L, 5L, 2L, 6L, 7L, 1L, 4L, 8L, 9L), .Label = c("CanCov", 
"Elevation", "Intercept", "NDVI", "Reggedness", "Slope", "SlopeSq", 
"Solar", "Snow"), class = "factor"), Covariate = structure(c(1L, 
2L, 4L, 5L, 7L, 9L, 11L, 13L, 1L, 3L, 4L, 6L, 8L, 10L, 12L, 14L, 
15L), .Label = c("Intercept", "SlopeVar_30Log", "VMR_30Log", 
"Ele_30", "Slope_500", "Slope_100", "Slope_500Sq", "Slope_100Sq", 
"CanCov_1000", "CanCov_30", "NDVI_MeanTin_1000", "NDVI_MeanAmp_1000", 
"RadSumm_30", "AspectCos_30", "SnowSWE_1000"), class = "factor"), 
    Beta = c(-4.06730774504316, 0.183591094124286, 0.61940812155272, 
    2.35498841169247, -0.672944306628242, -0.387903359930857, 
    -0.815906730978726, -0.351201997266259, -5.06483998950688, 
    0.454499369654063, -0.337997523846132, 2.10010144558015, 
    0.251723125039171, -0.281294023525884, -1.02515594862923, 
    -0.46350259164822, -0.498134174074173), SE = c(0.0499, 0.013, 
    0.0186, 0.0527, 0.0326, 0.0234, 0.0152, 0.0107, 0.113, 0.0126, 
    0.0255, 0.0427, 0.0228, 0.0222, 0.0271, 0.0197, 0.033), Season = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L), .Label = c("Summer", "Winter"), class = "factor")), .Names = c("General", 
"Covariate", "Beta", "SE", "Season"), row.names = c(NA, 17L), class = "data.frame")

0 个答案:

没有答案