Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xy plot between strings and numbers

Tags:

r

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
like image 978
SAN Avatar asked Nov 02 '11 08:11

SAN


1 Answers

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)

enter image description here


In lattice, you can use dotplot:

library(lattice)
dotplot(y~x)

enter image description here


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()

enter image description here

like image 152
Andrie Avatar answered Oct 23 '22 18:10

Andrie