# Scatter plot in R with non numeric data

I need to represent some non numeric data of a questionnaire in a scatter plot in R. What I mean by a non numeric data is that, I have two questions answers to which are some text. For example Q1 has following answers ("A", "B", "C") and Q2 has following answers ("X", "Y", "Z")

I need to represent these two questions (Q1 and Q2) in a scatter plot in order to show visually which persons who answered to Q1 with "A" answered to Q2 with "X" and so on. It would be better to represent like this:

Thank you.

• How does this graph show "A", "B", etc.? – Nick Cox Apr 24 '14 at 18:40
• Scatterplots don't make any sense in your situation. Try a mosaic plot instead. – gung - Reinstate Monica Apr 24 '14 at 18:42
• Presumably you can summarize your data as a two-way contingency table so that a level or fluctuation plot of frequencies, or a sieve or mosaic display as suggested by @gung, might be a better option. If you are interested in highlighting agreement between the two sets of responses, you can even try an agreement chart. For further readings, I recommend M. Friendly. – chl Apr 24 '14 at 19:09

## 1 Answer

I'm not sure I understood your question, but I hope the following helps.

First, let's make some data similar to what you might have in your hands:

> set.seed(3409)
> df <- data.frame(q1 = sample(LETTERS[1:3],   15, replace = TRUE),
+                  q2 = sample(LETTERS[24:26], 15, replace = TRUE))
> df
q1 q2
1   A  Z
2   C  Z
3   B  Z
4   B  X
5   C  Z
6   B  Y
7   C  X
8   C  X
9   A  Y
10  A  Y
11  C  Z
12  C  Z
13  C  Y
14  A  X
15  A  Z


Now we'll make the table that will base the graph we want to do later:

> tb <- table(df$q1, df$q2)
> tb

X Y Z
A 1 2 2
B 1 1 1
C 2 1 4


Two popular ways of plotting the data above are through a barplot and a mosaic plot:

> barplot(tb, beside = TRUE, legend = TRUE)  # barplot


> plot(tb)  # mosaic plot


As far as I know, scatterplots are not suited for categorical data.

• +1, if you call ?mosaicplot instead of plot, you can set shade=TRUE which will depict the Pearson residuals / helps you identify which cells differ from their expected values. – gung - Reinstate Monica Apr 25 '14 at 1:20
• @Waldir, thank you very much. This was more or less what I wanted to see. Could you please tell me how to put labels (showing their values) on each boxes in the mosaic plot (plot(tb)). Thanks a lot for your help. – Bakhtiyor Apr 25 '14 at 10:15
• @Bakhtiyor I believe that deserves it's own CV post, but I've taken a look around and read that you should take a look at the vcd package, which has a bunch of functions specially developed for Visualizing Categorical Data. I've tried working its mosaic and labeling_cells functions a bit, but with no success so far. In this case, it may help to use ftable(df$q1, df$q2) instead of table. – Waldir Leoncio Apr 25 '14 at 11:50
• All right, anyway a lot of thanks for your help. I highly appreciate it. – Bakhtiyor Apr 25 '14 at 15:39