Search code examples
rlistggplot2subsetr-factor

Use qplot to draw only one subset of data


I have a list named d like this:

V1 is an integer set from 0 - 50 V2 is a real set from 1500 - 1800 V3 is an integer set from 1 - 50

In total, the list contains 5100 objects

Now I would like to plot the histogram of V2, with V1 = a certain number (0, 1 or 10, etc.)

I tried different ways:

factor(d$V1)
qplot(V2, data=d, V1 = 1)        --> not successful 
d.subset <- subset(d, d$V1 = 1)  --> not successful

I really get crazy with this. Check the characteristics of d$V1 but found nothing strange. Anyone could help me out?

is.factor(d$V1) 

[1] TRUE

str(d$V1)  Factor w/ 51 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...

levels(d$V1)  
[1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" 
[20] "20" "21" "22" "23" "24" "25" "26" "27" "28" "29" "30" "31" "32" "33" "34" "35" "36" "37""38" 
[39] "39" "40" "41" "42" "43" "44" "45" "46" "47" "48" "49" "50" "51"

Solution

  • Change the line:

    d.subset <- subset(d, d$V1 = 1)
    

    to

    d.subset <- subset(d, V1 == 1)
    

    Notice the double equals (==) to denote the logical operator. = is used for assignment and doesn't subset the data frame.

    Finally, you might mean to put the 1 in quotes if you want to get the "1" level of the factor (which might not be the same as the numeric 1).

    d.subset <- subset(d, V1 == "1")