-1

I am trying to make a plot in R where I have Age on the x-axis, Location on the y-axis and where they meet I want the Days value in a color code. I have looked into different contour plots but I can't get them to accept non-numeric values.

What would you suggest?

Data example

Age    Location  Days
20-25     NY       5
20-25     BE       3
30-45     NY       4
46-50     CA       8
...
KoenV
  • 4,113
  • 2
  • 23
  • 38
Erik Warming
  • 184
  • 1
  • 2
  • 14
  • Please show us the code you tried so far. – LAP May 23 '17 at 12:42
  • I have tried a few function, `filled.contour` and `hexbin` but I must admit that I did not save code bits that did not work for me. I felt that I was on the wrong track, and that was why I posted my question – Erik Warming May 23 '17 at 13:14
  • I have found the plot-function that I was looking for. – Erik Warming May 29 '17 at 09:25
  • I have found the plot-function that I was looking for [https://stackoverflow.com/questions/5453336/plot-correlation-matrix-into-a-graph](The Correlation plot) Now I am struggling with getting the frequency table with Age as a column and Location as a row, and the frequency days in between – Erik Warming May 29 '17 at 09:35

1 Answers1

0

Using VCD package you may need to improve this much better (refer to 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))
Ajay Ohri
  • 3,382
  • 3
  • 30
  • 60