在R中绘制2个非数字和1个数字

时间:2017-05-23 12:36:18

标签: r plot contour

我想在R中制作一个情节,我在X轴上有Age,Y轴上的位置和它们相遇的地方我想要颜色代码中的Days值。我已经查看了不同的等高线图,但我不能让它们接受非数字值。

你会建议什么?

数据示例

Age    Location  Days
20-25     NY       5
20-25     BE       3
30-45     NY       4
46-50     CA       8
...

1 个答案:

答案 0 :(得分:0)

使用VCD包你可能需要更好地改进(请参阅http://www.datavis.ca/courses/VCD/vcd-tutorial.pdf

data1=NULL
> data1$age=as.factor(rep(c("20-25","25-30","30-45","46-50"),10)
+ )
> data1$age
 [1] 20-25 25-30 30-45 46-50 20-25 25-30 30-45 46-50 20-25 25-30 30-45 46-50
[13] 20-25 25-30 30-45 46-50 20-25 25-30 30-45 46-50 20-25 25-30 30-45 46-50
[25] 20-25 25-30 30-45 46-50 20-25 25-30 30-45 46-50 20-25 25-30 30-45 46-50
[37] 20-25 25-30 30-45 46-50
Levels: 20-25 25-30 30-45 46-50
> data1$location=as.factor(sample(c("NY","BE","CA"),40,T))
> data1$days=as.factor(sample(15,40,T))
> data1=as.data.frame(data1)
> str(data1)
'data.frame':   40 obs. of  3 variables:
 $ age     : Factor w/ 4 levels "20-25","25-30",..: 1 2 3 4 1 2 3 4 1 2 ...
 $ location: Factor w/ 3 levels "BE","CA","NY": 2 1 2 3 3 3 2 1 2 1 ...
 $ days    : Factor w/ 15 levels "1","2","3","4",..: 10 11 1 14 8 4 13 4 12 10 ...
> data1$days=as.numeric(data1$days)

> ftable(data1)
               days 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
age   location                                         
20-25 BE            0 0 0 0 0 0 0 0 0  0  0  0  0  0  0
      CA            1 0 0 0 0 1 0 0 0  1  0  2  0  0  0
      NY            0 0 0 0 0 0 0 1 1  0  2  0  0  1  0
25-30 BE            0 0 0 0 0 0 0 0 0  1  1  0  0  0  0
      CA            0 0 0 1 0 0 0 0 0  0  0  1  0  0  0
      NY            0 0 1 1 2 0 0 1 0  0  0  0  0  0  1
30-45 BE            0 1 0 0 0 0 1 1 0  0  1  0  0  1  0
      CA            2 0 0 0 0 0 0 0 0  0  0  0  1  0  0
      NY            0 0 0 0 0 0 0 1 0  0  0  0  0  1  0
46-50 BE            0 1 0 1 0 0 0 0 0  0  0  1  0  0  0
      CA            0 0 2 0 0 0 0 0 0  0  0  0  0  0  0
      NY            1 1 0 2 0 0 0 0 0  0  0  0  0  1  0


> mosaic(ftable(data1))

enter image description here

 > structable(data1)
               location BE CA NY
    age   days                  
    20-25 1              0  1  0
          2              0  0  0
          3              0  0  0
          4              0  0  0
          5              0  0  0
          6              0  1  0
          7              0  0  0
          8              0  0  1
          9              0  0  1
          10             0  1  0
          11             0  0  2
          12             0  2  0
          13             0  0  0
          14             0  0  1
          15             0  0  0
    25-30 1              0  0  0
          2              0  0  0
          3              0  0  1
          4              0  1  1
          5              0  0  2
          6              0  0  0
          7              0  0  0
          8              0  0  1
          9              0  0  0
          10             1  0  0
          11             1  0  0
          12             0  1  0
          13             0  0  0
          14             0  0  0
          15             0  0  1
    30-45 1              0  2  0
          2              1  0  0
          3              0  0  0
          4              0  0  0
          5              0  0  0
          6              0  0  0
          7              1  0  0
          8              1  0  1
          9              0  0  0
          10             0  0  0
          11             1  0  0
          12             0  0  0
          13             0  1  0
          14             1  0  1
          15             0  0  0
    46-50 1              0  0  1
          2              1  0  1
          3              0  2  0
          4              1  0  2
          5              0  0  0
          6              0  0  0
          7              0  0  0
          8              0  0  0
          9              0  0  0
          10             0  0  0
          11             0  0  0
          12             1  0  0
          13             0  0  0
          14             0  0  1
          15             0  0  0

    > mosaic(structable(data1))