I spent lot of time searching for it, but couldnt find. Dont blast me if this is a basic question :)
I want to generate a scatter plot with below vectors
> x
[1] "a" "b" "c" "d"
> y
[1] 5 6 3 4
I used xyplot, but it gives below errors
> xyplot(y~x)
Hit <Return> to see next plot:
Warning messages:
1: In order(as.numeric(x)) : NAs introduced by coercion
2: In diff(as.numeric(x[ord])) : NAs introduced by coercion
3: In function (x, y, type = "p", groups = NULL, pch = if (is.null(groups)) plot.symbol$pch else superpose.symbol$pch, :
NAs introduced by coercion
There are many ways of doing this. Here is one suggestion from each of the main graphics libraries, i.e. base graphics
, lattice
and ggplot2
:
In base graphics you can plot factor(x)
against y:
plot(factor(x), y)
In lattice
, you can use dotplot
:
library(lattice)
dotplot(y~x)
And with ggplot2
you can use either qplot
or ggplot
(after converting data to a data.frame
):
library(ggplot2)
qplot(x, y)
ggplot(data.frame(x, y), aes(x,y)) + geom_point()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With