Dnaiel
Dnaiel

Reputation: 7832

basic R scatter plot

I'd like to plot a scatter plot of X versus Y, but X is now of type character.

I.e.,

x = c("a", "b", "b", "c", "a")
y = c(9,2,4,5,1)

If i do,

plot(c(1,2,3,4,5), c(9,2,4,5,1))

scatterplot for x versus y with y numeric

The plot I show has X-axis numerated from 1 to 5.

Instead of that I want the x-axis to be the specific character series, i.e., "a", "b", "b", "c", "a". For some reason when I try to plot with the character it groups them so I get only a,b,c in the x-axis, and two values for a, two values for b and one for c.

Suggestions are welcome.

Thanks!

Upvotes: 1

Views: 585

Answers (1)

nico
nico

Reputation: 51680

This should do the job

plot(y, xaxt="n")
axis(1, at=1:length(x), labels=x)

Essentially you plot y hiding the x axis, then add a custom x axis to the plot.

See ?par and ?axis for more info.

Upvotes: 2

Related Questions