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

Richard M. Heiberger rmh at temple.edu
Wed Aug 27 16:09:06 CEST 2014


With lattice graphics, yes

libary(lattice)
xyplot(y ~ z, data=my.data)
bwplot(y ~ z, data=my.data)
stripplot(y ~ z, data=my.data)

With base graphics, probably not.

More importantly, don't use attach.  It will get you into trouble.
In this case, it didn't work for me because I had a variable named y
in my global environment.
Use the data= argument instead.

Your examples done with the data= argument are

plot(y ~ z, data=my.data)
plot(y ~ factor(z), data=my.data)
stripchart(y ~ z, vertical = TRUE, pch=19, data=my.data)

On Wed, Aug 27, 2014 at 9:42 AM, Luigi Marongiu
<marongiu.luigi at gmail.com> wrote:
> 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