[R] scatterplot using plot() function with factorial data

David L Carlson dcarlson at tamu.edu
Wed Aug 27 15:58:45 CEST 2014


It is, but stripchart() is simpler.

plot(y~as.numeric(z), my.data, xlab="x", xaxt="n", pch=19)
axis(1, 1:4, LETTERS[1:4])

If you want more spacing along the x-axis try

plot(y~as.numeric(z), my.data, xlab="x", xaxt="n", 
    xlim=c(.5, 4.5), pch=19)
axis(1, 1:4, LETTERS[1:4])

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352



-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Luigi Marongiu
Sent: Wednesday, August 27, 2014 8:42 AM
To: r-help at r-project.org
Subject: [R] scatterplot using plot() function with factorial data

Dear all,
I would like to ask whether is possible to draw a scatterplot using
the simple plot() function when the data is factorial. Without the
addition of the argument factor(), plot() represent the factorial data
on a linear scale whereas using this argument transforms plot() from a
scatterplot to a boxplot. Even adding factor directly in the
arrangement of the dataset does not alter the result.
The stripchart() function does the job I am looking for (essentially
draw the individual points of the dataset on proper axis reference,
that is categorical/factorial), but I was wondering whether is
possible to use plot() directly.
Best regards,
Luigi

----
my.data<-structure(list(
  row = 1:60,
  x = c(
0, 0, 0, 1, 1, 1, 2, 2, 2,
3, 3, 3, 4, 4, 4, 0, 0, 0,
1, 1, 1, 2, 2, 2, 3, 3, 3,
4, 4, 4, 0, 0, 0, 1, 1, 1,
2, 2, 2, 3, 3, 3, 4, 4, 4,
3, 3, 3, 0, 0, 0, 1, 1, 1,
2, 2, 2, 3, 3, 3, 4, 4, 4),
  y = c(
2073.928223, 2131.830067, 2131.830067, 0.143912883,
2191.348468, 2073.928223, 2117.20479, 2017.59903, 1896.388977,
1976.358448, 2003.757427, 1883.378928, 2283.756186,
2363.732429, 2315.416732, 2206.485917, 2191.348468,
2176.314869, 1990.010783, 2059.700178, 1976.358448,
617.4528799, 613.2168858, 617.4528799, 1686.950197,
1819.655315, 1832.225173, 1480.122531, 1298.652866,
1212.260417, 495.3736815, 505.7106218, 538.0337432,
383.9842946, 365.919416, 330.0195927, 505.7106218,
541.7503854, 498.7956356, 512.7214729, 584.3675585,
564.5956413, 604.8318804, 604.8318804, 592.4688595,
1272.107849, 1298.652866, 1298.652866, 1935.96084,
2088.254554, 1962.799773, 4452.994159, 4422.444691,
4128.243033, 312.3359691, 316.6659968, 332.2993098,
1500.642011, 1531.95584, 1430.042989),
  z = c(
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3)),
 row.names = c(NA, -60L),  class = "data.frame")
attach(my.data)
my.data$z<-factor(my.data$z, levels = c(0, 1, 2, 3))
levels(my.data$z)<-c("A", "B", "C", "D")

par(mfrow=c(1,3))  # 1 row, 2 columns
plot(y~z)
plot(y~factor(z))
stripchart(y~z, vertical = TRUE, pch=19)

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list