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.
qplot
. Use the data argument to define the dataset instead,data = diamonds
. Then refer to the variable names directly, e.g.,facets = .~cut
. - aosmithdf
example doesn't work for me ifx
andy
are only in the dataset and not in the global environment. Remove them and usedf = 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 thedata
argument inqplot
to avoid these issues. - aosmith