stevec
stevec

Reputation: 52168

ggplot qplot (quick plot) with fill?

How do you do a quick plot (i.e.qplot) with fill?

I've tried

iris %>% qplot(Sepal.Length, fill = Species)
Error in FUN(X[[i]], ...) : object 'Sepal.Length' not found

and

iris %>% qplot(seq_along(Sepal.Length), Sepal.Length, fill = Species)
Error in FUN(X[[i]], ...) : object 'Sepal.Length' not found

But no luck.

Upvotes: 0

Views: 235

Answers (2)

O.A
O.A

Reputation: 141

The error code is because of the %>%.

The pipe doesn't work because it by default places the lhs as the first argument in the rhs, in the case of qplot, this is x, rather than data. If you still wanted to use %>%, you would need to specify which argument it is to pipe to:

iris %>% qplot(data =., Sepal.Length, fill = Species)

and, in your second version, but with @sahwahn's correction

iris %>% qplot(data=., seq_along(Sepal.Length), Sepal.Length, color = Species)

Upvotes: 3

user438383
user438383

Reputation: 6206

If you are looking to plot a histogram and colour by species, pass Species to the colour argument.

qplot(data=iris, x=Sepal.Length, fill=Species)

enter image description here

Otherwise, if you were looking to plot points:

qplot(data=iris, x=seq_along(Sepal.Length), y=Sepal.Length, colour=Species)

enter image description here

Upvotes: 1

Related Questions