Share |

3d scatter plot using R

The other day I saw a three dimensional scatterplot in Montgomery's Regression book. I wanted to redraw the graph using the provided data. A simple google search revealed that there is a package called scatterplot3d. The scatterplot3d() can be used to draw a 3-dimensional scatter plot. Here is what the steps are:

Download and install the package from your nearest CRAN.
Load the package using the command library(scatterplot3d)

Use the attached file to run the following code in R

d1<-read.table("65-555-reg.txt", header=T)
d2<-data.frame(time=c(d1[,1]), cases=c(d1[,2]), distance=c(d1[,3]))
attach(d2)
scatterplot3d(cases, distance, time, angle=20, col.axis="blue",
col.grid="lightblue", main="Three-dimensional scatterplot",
pch=21, box=F, cex.symbols=2)
detach(d2)

RESULT
3dscatterplot.jpeg

DATA
Data: Copy and paste in a file and save it as 65-555-reg.txt

time	 cases	distance
16.68	7	560
11.5	3	220
12.03	3	340
14.88	4	80
13.75	6	150
18.11	7	330
 8	2	110
17.83	7	210
79.24	30	1460
21.5	5	605
40.33	16	688
21	10	215
13.5	4	255
19.75	6	462
24	9	448
29	10	776
15.35	6	200
19	7	132
9.5	3	36
35.1	17	770
17.9	10	140
52.32	26	810
18.75	9	450
19.83	8	635
10.75	4	150


Comments

Dear Sir...Sometimes we wonder where you get so much of passion for Statistics from!!! :)

That's a great complement, Aniqa!
There are tons of more things to know.. life is way too short to know all of them. I try to know new things and share with others. I find joy in doing so.

Now I understand why Latif sir always tells us, we can do anything in R, if we can give it a proper command.

How do I get the row numbers as label on the 3d points for the example you have given on http://www.stattler.com/blog/3d-scatter-plot-using-r

Good day!
I've been surfing the net for long hours to look for R command in plotting 2D and 3Dimensional solution space... Fortunately, i found this page..
I just want to ask if there is R command to plot the data points and make use of symbols as group indicator.. to come up with a conclusion that a points of the same symbol belongs to the same group.. something like that.. it find me hard to do so..
Any suggestions please.. any help will do and very much appreciated.. THANKS!

I am not certain if that can be done with a 3D plot, but I am pretty sure it is possible with 2D plot with matplot()

For example, I am creating a matrix with three columns, each representing uniform random integers within certain range.

> x=matrix(c(round(runif(15), digits=0), round(runif(15, 8, 9), digits=0), round(runif(15, 14, 15), digits=0)), ncol=3)
> matplot(x)

This will give you a figure with the values of the columns as the plotting points.

Thanks a lot sir!
Somehow it helps me...
I forgot to say about classical multidimensional scaling..it makes use of data arranged in a square matrix transformed into Euclidean Distances..i'm sure you know these stuffs sir..^^
when i plot the 2D solution space, i make use the variable names instead to label the points..but, the output seems crowded and not clear due to some points that group together.. i want to use if there's any R command that make use of symbols not the variable names.. to come up easily with a conclusion that those points with the same symbols are similar and dissimilar otherwise...
Is there anything like that sir in R?
Thanks for your replies sir..and time granting these messages..
I know it's kinda elementary issues but i don't know much about R..
a sort of dissertation in our school.. ^_^
thank you again sir!

I believe, that will be possible using the same tool you are using to plot the figures. Could you please post some codes and/or examples that you are attempting? Then, I, or someone else could find a way for you.

When transforming the data matrix into euclidean distances

>D=dist(data,method="euclidean" ,diag=TRUE,upper=TRUE)

When doing the scaling

> cmdscale(D,k = 2,eig = TRUE,add = FALSE,x.ret = FALSE)

Defining variables for plotting

> loc <- cmdscale(D)
> x <- loc [, 1]
> y <- loc [, 2]

Plotting 2D solution space that outputs plots with variable names as labels..

>plot(x,y,xlab="Dimension1",ylab="Dimension2",xlim=range(x)*1.2,type="n")
>text(x,y,labels=colnames(datafilename))

I want to use symbols as data point labels in the graph instead of the variable names.. thanks.

do u have any suggestions about labeling using symbols instead of variable names?..

I will look into this, and will post the results if successful.

Update:

You can simply plot x and y and use a plotting character. The code will be like this:

plot(x, y, pch=10)

To see a list of symbols, visit http://voteview.com/symbols_pch.htm

thanks..
but what i wish to come up with the symbols is to have a groupings of them, not using only one symbol, so that points having the same symbols on the map will be perceived as similar..
Expected that there are different kinds of symbols on the map representing different groups of points with similarities..
^^ is that possible in R?

That is certainly possible with R, or perhaps, in any programming environment. The problem is, you have to define your own criteria of "clustering", i.e., on what basis you want to call a group numbers as clusters. Once you decide those criteria, you can assign them a specific plotting symbol, and plot them!

There could be functions already available to achieve this, which I do not know at the moment. You may try searching with keywords such as "cluster plot with R", "visualize cluster in data using R" and so on.

If you find the solution, please share with us. Thanks. :)

I think this isn't possible and disadvantage in multidimensional scaling... the one that you mention is another thing.. I guess Cluster Analysis:D ^_^
it is an honor to post and share something statistics issues on this page..THANKS :)

Hi,
This is excellent. I wonder if there is a function to copy and past the graph to a word document
CHeers
Rajeev

i guess just do the Right Click on the graph you want to copy and select Copy as Metafile..:D that's what i did.

Anyone who knows quality control procedures in R?

Hello,
I just downloaded the files but have no idea of how to install it. Qhen I opened the RAR file it copied a folder to my library R folder, but I can´t find a executable. Because R does not recognize it if I only copy teh files to the library.
Thank you very much,
Ricardo

Assuming that you are using R in an Windsor environment, you will have to use the following command to see if it has been installed.

library(scatterplot3d)

If you see any error, that will indicate that the package was not installed.

To install the package, you need to follow the instructions at http://rwiki.sciviews.org/doku.php?id=getting-star...

Hello Mr. Raheem! I just extracted the files in the folder scatterplot3d into the R library folder. What else do I need to do so R can recognize the package. Thank you very much
Ricardo

Did you follow the link that I gave you in the above comment? That has instructions how to install package. Ideally, you should not extract any file anywhere in the R folder. Rather download and save the ZIP file somewhere else.

Then in the R menu, click on install packages from local zip file.

But a simpler way to install package is to run the following command:

Make sure to run R as an administrator. To do so, right-click on the R-icon, then select "Run as Administrator"

Then, in the R command prompt, type

install.packages("scatterplot3d")

This will bring a dialogue box to select a mirror site. Ideally, you should select the mirror nearest to you. Anyone will work though. Then click OK.

Follow the instructions. You should be fine.

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <div>
  • LaTex commands embedded in text will be interpreted and rendered. Additional information can be found at DruTex Documentation Pages
    • Assists automatic numbering of tex, equation, and equations environments.
    • Provides different environments to create rendered images (especially maths).
    • You may use <swf file="song.mp3"> to display Flash files inline
    • You can use BBCode tags in the text. URLs will automatically be converted to links.
    • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <drupal6>, <latex>, <rsplus>, <sas>, <text>. The supported tag styles are: <foo>, [foo].

    More information about formatting options

    By submitting this form, you accept the Mollom privacy policy.

Google Video

Loading...
Loading...