BluePanda
BluePanda

Reputation: 13

x-axis label as text not number

I have found ways to do this but they seem to work only in the case when there is only one input for every output.

I have a scatter plot and I'd like to use the X values (which are string values) as the x-axis values rather than numbers.

As an example data set:

cookie1, 2
cookie1, 3
cookie1, 4
cookie2, 2
cookie2, 4
cookie2, 4

So I would like the x axis to read cookie1, cookie2, etc...while the y axis maintains the rest of the scatter plot for the values (in this case 2-4).

Right now, as it plots out, instead of cookie1, or cookie2 in the x-axis it reads 1,2,3,etc...

I have tried making a vector of values housing cookie1, cookie2, etc individually, but that blows up in my face too.

Any suggestions?

Upvotes: 0

Views: 3373

Answers (1)

Rich Scriven
Rich Scriven

Reputation: 99331

You can set the x-axis labels to null and then add the labels from the data with axis.

d <- read.table(text = 'cookie1, 2
 cookie1, 3
 cookie1, 4
 cookie2, 2
 cookie2, 4
 cookie2, 4', sep = ',')

> plot(d$V2, xaxt = 'n', pch = 19)
> axis(1, at = seq(nrow(d)), labels = d$V1)

enter image description here

Upvotes: 3

Related Questions