ggplot geom_segment()未添加

时间:2018-10-16 09:13:27

标签: r ggplot2

我正在用ggplot绘制dbRDA排序。

数据:

df1 <- structure(list(dbRDA1 = c(-0.277603544609864, -0.360978808436824, 
-0.233134911000267, -0.310476800969782, -0.0906489101103515, 
-0.145001415653946, -0.0332934057167847, -0.0275012617442552, 
1.09301352917172, 1.06620520659396, -0.706049468959193, -0.322962763102308, 
0.0621994470608108, 0.286233107477088), dbRDA2 = c(-0.0544365381093796, 
-0.396097172755407, -0.179335397101395, -0.181894682898319, -0.0332360598060943, 
-0.133901893994658, -0.231316948737708, -0.104267201073184, -0.446406664141594, 
-0.324349265794935, 0.218701171555884, -0.0300275899654245, 0.595616680358468, 
1.30095156246375), place = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("N", "O"), class = "factor"), 
    placecol = c("hollow", "hollow", "hollow", "hollow", "hollow", 
    "hollow", "hollow", "hollow", "non-hollow", "non-hollow", 
    "non-hollow", "non-hollow", "non-hollow", "non-hollow"), 
    treat = structure(c(2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 
    2L, 2L, 1L, 1L), .Label = c("10", "A"), class = "factor"), 
    OTU = c(1.358, 5.94, 1.119, 2.113, 1.12, 0.604, 1.525, 0.712, 
    1.841, 3.203, 2.512, 1.087, 1.087, 1.506)), .Names = c("dbRDA1", 
"dbRDA2", "place", "placecol", "treat", "OTU"), row.names = c("60759", 
"60773", "60774", "60775", "60776", "60777", "60778", "60779", 
"60780", "60781", "60782", "60783", "60784", "60785"), class = "data.frame")

并绘制代码:

rda.plot <- ggplot(df1, aes(x=dbRDA1, y=dbRDA2, color=factor(treat),shape=factor(place))) + 
  geom_point(size=sqrt(df1$OTU)/sqrt(max(df1$OTU))*10,
             aes(fill=factor(treat), alpha = as.factor(place)))+
  geom_point(size=sqrt(df1$OTU)/sqrt(max(df1$OTU))*10) +    
  geom_text(aes(label=row.names(df1)),size=3, position = position_nudge(y = -0.1)) + 
  geom_hline(yintercept=0, linetype="dotted") +
  geom_vline(xintercept=0, linetype="dotted") +
  scale_shape_manual(values=c(21,24)) +
  scale_alpha_manual(values=c(1,0)) +
  scale_colour_manual(values = c("indianred3","lightskyblue4"))+
  scale_fill_manual(values =alpha(c("indianred3","lightskyblue4")))+  
  coord_fixed() 
rda.plot

如图所示,我有很多缩放比例和颜色代码,它们可以正常工作,但是(至少对我自己而言)令人困惑。它来自遵循一系列不同的准则(我想可以对其进行修剪)。

我想添加一个与环境矢量相对应的最后一层。

环境数据框:

vf_biplot <- structure(list(dbRDA1 = c(0.703799726303485, 0.108418920195407, 
0.10268149348934, 0.393538121176574, -0.692749765298912), dbRDA2 = c(-0.507323360489587, 
0.843331735032726, -0.549310027554416, -0.36248490838257, -0.291382798048848
), Envir = c("AA", "N", 
"DOP", "Pm", "P")), .Names = c("dbRDA1", "dbRDA2", 
"Envir"), row.names = c("AA", "N", 
"DOP", "Pm", "P"), class = "data.frame")

我尝试添加图层(受stackoverflow启发):

rda.plot + geom_segment(data = vf_biplot,
                       aes(x = 0, xend = dbRDA1, y = 0, yend = dbRDA2),
                       arrow = arrow(length = unit(0.25, "cm")), colour = "grey")+
  geom_text(data = vf_biplot, aes(x = dbRDA1, y = dbRDA2, label = Envir),
            size = 3)

我收到此错误:

Error in sort.list(y) : 'x' must be atomic for 'sort.list'
Have you called 'sort' on a list?

我用str()进行了检查,但vf_biplotdf1都不是列表(或包含列表)。

0 个答案:

没有答案