views:

75

answers:

2

I have results from a survey. I am trying to create a graphic displaying the relationship of two variables: "Q1" and "Q9.1". "Q1" is the independent and "Q9.1" is the dependent. Both variables have responses from like scale questions: -2,-1,0,1,2. A typical plot places the answers on top of each other - not very interesting or informative. I was thinking that hexbin would be the way to go. The data is in lpp. I have not been able to use "Q1" and "Q9.1" for x and y. However:

> is.numeric("Q1")
[1] FALSE
q1.num <- as.numeric("Q1")
Warning message:
NAs introduced by coercion 

The values for Q1 are (hundreds of instances of): -2,-1,0,1,2

How can I make a hexbin graph with this data? Is there another graph I should consider?

Error messages so far:

Warning messages:
1: In xy.coords(x, y, xl, yl) : NAs introduced by coercion
2: In xy.coords(x, y, xl, yl) : NAs introduced by coercion
3: In min(x) : no non-missing arguments to min; returning Inf
4: In max(x) : no non-missing arguments to max; returning -Inf
5: In min(x) : no non-missing arguments to min; returning Inf
6: In max(x) : no non-missing arguments to max; returning -Inf
+3  A: 

How about taking a slightly different approach? How about thinking of your responses as factors rather than numbers? You could use something like this, then, to get a potentially useful representation of your data:

# Simulate data for testing purposes
q1 = sample(c(-2,-1,0,1,2),100,replace=TRUE)
q9 = sample(c(-2,-1,0,1,2),100,replace=TRUE)
dat = data.frame(q1=factor(q1),q9=factor(q9))
library(ggplot2)
# generate stacked barchart
ggplot(dat,aes(q1,fill=q9)) + geom_bar()

You may want to switch q1 and q9 above, depending on the view of the data that you want.

seandavi
This produced one solid bar.
Donnied
My apologies... Works great! Thank you.
Donnied
+2  A: 

Perhaps ggplot2's stat_binhex could sort that one for you?

Also, I find scale_alpha useful for dealing with overplotting.

radek
I really like the stat_binhex. I can't find how to add title though. labs, xl, and yl don't work.
Donnied
to label x axis you could try: qplot(x, y, data = data, xlab = "my label") or: ggplot(data, aes(x, y)) + geom_point() +scale_x_continuous("my label")
radek
The `ylab()` and `xlab()` functions also add axis labels, e.g. `ggplot(data, aes(x, y)) + geom_point() + ylab("my label")`
Gavin Simpson