我如何在R中绘制一棵树(和松鼠)? [英] How can I plot a tree (and squirrels) in R?

查看:289
本文介绍了我如何在R中绘制一棵树(和松鼠)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  tree = data.frame(branchID = c(1,11,12,111,112,1121, 1122),长度= c(32,21,19,5,12,6,2))

>树
branchID长度
1 1 32
2 11 21
3 12 19
4 111 5
5 112 12
6 1121 6
7 1122 2

这棵树是2D的,由分支组成。每个分支都有一个ID。 1 是中继线。然后,树干分叉成两个分支,左侧为 11 ,右侧为 12 11 在分支中称为 111 (向左)和 112 (往右走)。每个分支都有一定的长度。



在这棵树上有松鼠:

  squirrels = data.frame(branchID = c(1,11,1121,11,111),PositionOnBranch = c(23,12,4,2,1),name = c(FluffyTail,Ginger ,NutCracker,SuperSquirrel,ChipnDale))

>松鼠
branchID PositionOnBranch名
1 1 23 FluffyTail
2 11 12生姜
3 1121 4 NutCracker
4 11 2 SuperSquirrel
5 111 1 ChipnDale

每只松鼠都在特定分支上找到。例如 FluffyTail 位于中继线的23号位置(中继线的总长度为32)。 ChipnDale 在位置1的分支 111 上(分支的总长度 111 是5)。该位置相对于分支的下端。



我该如何绘制我的树和我的松鼠?

解决方案

我把更多的思想/时间放在这里,并且在包,软件包的@ baptiste热点小贴士之后,现在是时候将这些点升级为松鼠(尽管它们可能类似于咖啡豆)。

 库(rphylopic)
s< ; - seed(50,10,min.branch.length = 0,max.branch.length = 5,
min.trunk.height = 5,max.trunk.height = 8)
g< (paste0('darkolivegreen',c('',1:4)))(100)$ b $ (g,2000,4,pch = 24:25,col = NA,cex = 1.2,bg = paste0(leafygreens,'50'))
xy < - foliate(g, 4,xy = TRUE,plot = FALSE)

#snazzy阴影
add_phylopic_base(
image_data(5ebe5f2c-2407-4245-a8fe-3 97466bb06da,size =64)[[1]],
1,xy $ x,xy $ y,ysize = 2.3,col ='black')
add_phylopic_base(
image_data (5ebe5f2c-2407-4245-a8fe-397466bb06da,size =64)[[1]],
1,xy $ x,xy $ y,ysize = 2,col ='darkorange3')


Here is my tree:

tree = data.frame(branchID = c(1,11,12,111,112,1121,1122), length = c(32, 21, 19, 5, 12, 6, 2))

> tree
  branchID length
1        1     32
2       11     21
3       12     19
4      111      5
5      112     12
6     1121      6
7     1122      2

This tree is in 2D and is made of branches. Each branch has an ID. 1 is the trunk. Then the trunk bifurcate into two branches, 11 on the left and 12 on the right. 11 bifurcates as well in the branches called 111 (going toward the left) and 112 (going toward the right). etc.. Each branch has a certain length.

On this tree there are squirrels:

squirrels = data.frame(branchID = c(1,11,1121,11,111), PositionOnBranch = c(23, 12, 4, 2, 1), name=c("FluffyTail", "Ginger", "NutCracker", "SuperSquirrel", "ChipnDale"))

> squirrels
  branchID PositionOnBranch          name
1        1               23    FluffyTail
2       11               12        Ginger
3     1121                4    NutCracker
4       11                2 SuperSquirrel
5      111                1     ChipnDale

Each squirrel is found on a specific branch. For example the FluffyTail is on the trunk at position 23 (the total length of the trunk being 32). ChipnDale is on the branch 111 at position 1 (the total length of the branch 111 is 5). The position is taken relatively to the lower extremity of the branch.

How can I plot my tree and my squirrels?

解决方案

I put a bit more thought/time into this, and have packaged up some horticultural functions in package trees, here.

With trees, you can:

  • generate a random tree design (a random seed, so to speak) with seed();
  • sow the seed to give rise to a magnificent tree with germinate();
  • add randomly-located leaves (or squirrels) with foliate();
  • add squirrels (for example) to specified locations with squirrels(); and
  • prune() the tree.

# Install the package and set the RNG state
devtools::install_github('johnbaums/trees')
set.seed(1)

Let's fertilise a seed and grow a tree

# Create a tree seed    
s <- seed(70, 10, min.branch.length=0, max.branch.length=4,
          min.trunk.height=5, max.trunk.height=8)

head(s, 10)

#       branch    length
# 1          0 6.3039785
# 2          L 2.8500587
# 3         LL 1.5999775
# 4        LLL 1.3014086
# 5       LLLL 3.0283486
# 6      LLLLL 0.8107690
# 7     LLLLLR 2.8444849
# 8    LLLLLRL 0.4867677
# 9   LLLLLRLR 0.9819541
# 10 LLLLLRLRR 0.5732175

# Germinate the seed
g <- germinate(s, col='peachpuff4')

And add some leaves

leafygreens <- colorRampPalette(paste0('darkolivegreen', c('', 1:4)))(100)
foliate(g, 5000, 4, pch=24:25, col=NA, cex=1.5, bg=paste0(leafygreens, '30'))

Or some squirrels

plot(g, col='peachpuff4')
squirrels(g, 
          branches=c("LLLLRRRL", "LRLRR", "LRRLRLLL", "LRRRLL", "RLLLLLR", 
                     "RLLRL", "RLLRRLRR", "RRRLLRL", "RRRLLRR", "RRRRLR"),
          pos=c(0.22, 0.77, 0.16, 0.12, 0.71, 0.23, 0.18, 0.61, 0.8, 2.71),
          pch=20, cex=2.5)

Plotting @Remi.b's tree and squirrels

g <- germinate(list(trunk.height=32, 
                   branches=c(1, 2, 11, 12, 121, 122),
                   lengths=c(21, 19, 5, 12, 6, 2)), 
              left='1', right='2', angle=40)

xy <- squirrels(g, c(0, 1, 121, 1, 11), pos=c(23, 12, 4, 2, 1), 
               left='1', right='2', pch=21, bg='white', cex=3, lwd=2)
text(xy$x, xy$y, labels=seq_len(nrow(xy)), font=2)
legend('bottomleft', bty='n',
      legend=paste(seq_len(nrow(xy)), 
                   c('FluffyTail', 'Ginger', 'NutCracker', 'SuperSquirrel', 
                     'ChipnDale'), sep='. '))


EDIT:

Following @baptiste's hot tip about @ScottChamberlain's rphylopic package, it's time to upgrade those dots to squirrels (though they may resemble coffee beans).

library(rphylopic)
s <- seed(50, 10, min.branch.length=0, max.branch.length=5,
          min.trunk.height=5, max.trunk.height=8)
g <- germinate(s, trunk.width=15, col='peachpuff4')
leafygreens <- colorRampPalette(paste0('darkolivegreen', c('', 1:4)))(100)
foliate(g, 2000, 4, pch=24:25, col=NA, cex=1.2, bg=paste0(leafygreens, '50'))
xy <- foliate(g, 2, 2, 4, xy=TRUE, plot=FALSE)

# snazzy drop shadow
add_phylopic_base(
    image_data("5ebe5f2c-2407-4245-a8fe-397466bb06da", size = "64")[[1]], 
    1, xy$x, xy$y, ysize = 2.3, col='black')
add_phylopic_base(
    image_data("5ebe5f2c-2407-4245-a8fe-397466bb06da", size = "64")[[1]], 
    1, xy$x, xy$y, ysize = 2, col='darkorange3')

这篇关于我如何在R中绘制一棵树(和松鼠)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆