Reputation: 338
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.
Upvotes: 0
Views: 728
Reputation: 338
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)
Upvotes: 1