0

First of all that's my data:

structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 3.9, 
6.4, 7.4, 8.1, 9, 9.4, 7.8, 12.8, 14.8, 16.2, 18, 18.8), .Dim = c(6L, 
22L), .Dimnames = list(c("Merc", "Peug", "Fera", "Fiat", "Opel", 
        "Volv"), c("10", "33.95", "58.66", 
"84.42", "110.21", "134.16", "164.69", "199.1", "234.35", "257.19", 
"361.84", "432.74", "506.34", "581.46", "651.71", "732.59", "817.56", 
"896.24", "971.77", "1038.91", "Reduction", "Price")))

To explain easier what I would like to achieve I will show the matrix:

> head(data)
     10 33.95 58.66 84.42 110.21 134.16 164.69 199.1 234.35 257.19 361.84 432.74 506.34 581.46 651.71 732.59 817.56 896.24 971.77 1038.91 Reduction Price
Merc  0     0     0     0      0      0      0     0      0      0      0      0      1      0      0      0      0      0      0       0 3.9      7.8
Peug  0     0     0     0      0      0      0     0      0      0      0      0      0      0      0      0      0      0      1       0 6.4     12.8
Fera  0     0     0     0      0      0      0     0      0      0      0      0      0      0      0      0      0      0      1       0 7.4     14.8
Fiat  0     0     0     0      0      0      0     0      0      0      0      0      0      0      0      0      1      0      1       0 8.1     16.2
Opel  0     0     0     0      0      0      0     0      0      0      0      0      0      0      0      0      0      0      0       0 9.0     18.0
Volv  0     0     0     0      0      0      0     0      0      0      0      0      0      0      0      1      0      0      1       0 9.4     18.8

As you see all of the rows have just two types of numbers (0 and 1). Sometimes it can be more than 1 in each row. On the xaxis I would like to put the numbers from Reduction column and for yaxis the column names should be used. Both axis should be scaled from 0 to 1200.

Now the tricky part. I would like to put on the graph the values from the rows where you can find number 1. If there is more than one 1 in each row, there should be more than one dot on the graph.

Rechlay
  • 1,457
  • 2
  • 12
  • 19

2 Answers2

4

I really like using dplyr for these kind of things since it keeps your code both compact and easy to read, even if you pick it up three months from now.

require(dplyr)
require(tidyr)
require(ggplot2)

d <- data %>%
    as.data.frame %>%
    mutate(Maker = rownames(data)) %>%
    gather(Column, Bool, -Maker, -Reduction, -Price) %>%
    filter(Bool == 1) %>%
    mutate(Column = as.numeric(levels(Column))[Column]) # Is factor otherwise

ggplot(d, aes(x=Reduction, y=Column, shape=Maker)) +
    geom_point() +
    scale_x_continuous(limits=c(0, 1200), breaks=c(0, 400, 800, 1200)) +
    scale_y_continuous(limits=c(0, 1200), breaks=c(0, 400, 800, 1200))

enter image description here

Backlin
  • 14,612
  • 2
  • 49
  • 81
  • That's ok but with scaling I meant that I would like to see the value from 0 to 1200 on `y and x axis`. I know that in that case the dots might not be visible because all of them will be completely on the left side. My data is much longer than the example which I showed and there will be higher values for the `xaxis` as well. I would like to see on both axis numbers like 0, 400,800,1200. That's what I meant by scaling. – Rechlay Mar 26 '15 at 10:25
  • 2
    Ok, I thought I must have misunderstood when there was so much blank space on the right, but if you got additional data it makes sense. – Backlin Mar 26 '15 at 10:54
  • That's what I would like to achieve but when I try to do it on my full data it gives me an error `Error: Discrete value supplied to continuous scale`. I know that's my fault because on given example it works perfect but maybe you have any idea what's wrong ? – Rechlay Mar 26 '15 at 11:04
  • That's odd. It happens if either `Reduction` or `Column` is a factor instead of a numeric, which could happen for example if you put a character vector in a data frame without using `stringsAsFactors=FALSE`. Try `aes(x=as.numeric(levels(Reduction))[Reduction], y=as.numeric(levels(Column))[Column])` -- [more info here](http://stackoverflow.com/questions/3418128/how-to-convert-a-factor-to-an-integer-numeric-without-a-loss-of-information). – Backlin Mar 26 '15 at 12:55
1

I think following should solve you problem,

df = structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 
            0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 3.9, 
            6.4, 7.4, 8.1, 9, 9.4, 7.8, 12.8, 14.8, 16.2, 18, 18.8), 
          .Dim = c(6L,  22L), 
          .Dimnames = list(c("Merc", "Peug", "Fera", "Fiat", "Opel", 
                             "Volv"), 
                           c("10", "33.95", "58.66", 
                             "84.42", "110.21", "134.16", "164.69", "199.1", "234.35", "257.19", 
                             "361.84", "432.74", "506.34", "581.46", "651.71", "732.59", "817.56", 
                             "896.24", "971.77", "1038.91", "Reduction", "Price")))
df = as.data.frame(df)
df$Price = NULL
library(reshape)
meltDF = melt(df, id.vars = 'Reduction')
library(ggplot2)
ggplot(meltDF[meltDF$value == 1,]) + geom_point(aes(x = Reduction, y = variable))

enter image description here

vrajs5
  • 4,066
  • 1
  • 27
  • 44