[R] scatterplot using plot() function with factorial data
Ista Zahn
istazahn at gmail.com
Wed Aug 27 16:15:21 CEST 2014
Hi Luigi,
See in line.
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")
Your data.frame is broken; x has 63 values, the rest have only 60.
> attach(my.data)
don't do that, it screws you up later on
> my.data$z<-factor(my.data$z, levels = c(0, 1, 2, 3))
If your are expecting this to modify the attached value of z you will
be disappointed. It does not.
> levels(my.data$z)<-c("A", "B", "C", "D")
ditto
>
> par(mfrow=c(1,3)) # 1 row, 2 columns
> plot(y~z)
this is finding the attached value of z, which is not a factor
> plot(y~factor(z))
> stripchart(y~z, vertical = TRUE, pch=19)
once you fix your data and get rid of attach, something like
with(my.data, plot(y ~ as.numeric(z), xaxt = "n")
axis(1, 1:4, labels = levels(z)))
should do it.
Best,
Ista
>
> ______________________________________________
> 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