Luigi Marongiu
2014-Aug-27 13:42 UTC
[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)
David L Carlson
2014-Aug-27 13:58 UTC
[R] scatterplot using plot() function with factorial data
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.
Richard M. Heiberger
2014-Aug-27 14:09 UTC
[R] scatterplot using plot() function with factorial data
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.
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.