0
votes

I am trying to do a histogram with this data for an example of using ggplot2 to create graphics, and then i found out i cant do this histogram with facets using qplot command, i already do this kind of plot with another data, but now i trying again with this specific data i can not do it.

this is the code:

library(ggplot2)
qplot(x      = diamonds$price,
      geom   = "histogram",
      facets = .~diamonds$cut)

As you can see is actually really simple but it give me the error:

Error: value for ‘cut’ not found

if you do a quick research you find out that there are price values for every level in the cut factor.

diamonds$price[diamonds$cut=="Fair"]
diamonds$price[diamonds$cut=="Good"]
diamonds$price[diamonds$cut=="Very Good"]
diamonds$price[diamonds$cut=="Premium"]
diamonds$price[diamonds$cut=="Ideal"]

i couldn't understand what is wrong.

this is another example. But this is working.

x <- rnorm(120,20,20)
y <- as.factor(c(rep("yo",60),rep("tu",60)))
df <- data.frame(x,y)
qplot(x = df$x, geom = "histogram", facets = .~df$y)

What is the different between this data? i can not see it.

This show me that variables class are the same in this two example

is.numeric(diamonds$price)

[1] TRUE

is.numeric(x)

[1] TRUE

is.factor(diamonds$cut)

[1] TRUE

is.factor(y)

[1] TRUE

Please help.

1
The problem seems to stem from using dollar sign notation in qplot. Use the data argument to define the dataset instead, data = diamonds. Then refer to the variable names directly, e.g., facets = .~cut. - aosmith
qplot actually support the use of vector inside of a dataframe using the $ simbol. i will edit the post to show that. - Victor Espinoza
your solution actually give me the plot but i still not understand what is wrong with the code, why works with the second example but not the first one?. Thanks for your help. - Victor Espinoza
Your df example doesn't work for me if x and y are only in the dataset and not in the global environment. Remove them and use df = data.frame(x = rnorm(120,20,20), y = as.factor(c(rep("yo",60),rep("tu",60)))) to see the difference. You should be using the data argument in qplot to avoid these issues. - aosmith
You are right, i never notice that. i did suppose qplot was reading the df$x and df$y, but is not, now i get it. Thanks, a lot. I really appreciate this. - Victor Espinoza

1 Answers

1
votes

The following answer is based in aosmith help, thanks.

The problem is that qplot is actually not reading the df$x and df$y variables in example number 2:

library(ggplot2)    
x <- rnorm(120,20,20)
y <- as.factor(c(rep("yo",60),rep("tu",60)))
df <- data.frame(x,y)
qplot(x = df$x, geom = "histogram", facets = .~df$y)

In this example, qplot is readind the object x and y, created in the first two lines, it never use df$x or df$y.

So, in the example number 1:

qplot(x      = diamonds$price,
      geom   = "histogram",
      facets = .~diamonds$cut)

There is not price or cut object in the environment, that is why i get the error.

Solution: Using the argument data =

like this:

qplot(data = diamonds,
      x = price,
      geom = "histogram",
      facets = .~cut)