Dnaiel Dnaiel - 1 year ago 113
R Question

basic R scatter plot

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


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.


Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download