[R] smoothScatter plot

John Kane jrkrideau at inbox.com
Sat Oct 6 18:00:20 CEST 2012


Hi Zhengyu,

You might want to have a look at http://gallery.r-enthusiasts.com/graph/Scatterplots_with_smoothed_densities_color_representation,139
which seems to be showing a smoothScatter() that seems like what you want.  

I've never used the  function so I am probably not much help

Something else that I thought of, late yesterday, was the ggplot2 approach shown using this code.  
d <- ggplot(diamonds, aes(carat, price)) 
d + geom_point()  # graph all points with similar colour
d + geom_point(alpha = 1/10)  # graph points with transparency setting 

The alpha settings may give you something similar to smoothScatter() but probably without the colours though a question on the google groups ggplot2 group might help.

Good luck

Good luck,

John Kane
Kingston ON Canada

-----Original Message-----
From: zhyjiang2006 at hotmail.com
Sent: Sat, 6 Oct 2012 01:01:41 +0800
To: jrkrideau at inbox.com
Subject: RE: [R] smoothScatter plot

Hi John,

Thanks for your link. Those plots look pretty but way too complicated in terms of making R code.  

Maybe my decription is not clear.  But could you take a look at the attached png? I saw several publications showing smoothed plots like this but not sure how to make one...

Thanks,
Best,
Zhengyu

Date: Fri, 5 Oct 2012 06:36:38 -0800
From: jrkrideau at inbox.com
Subject: RE: [R] smoothScatter plot
To: zhyjiang2006 at hotmail.com
CC: r-help at r-project.org

 In line 

John Kane
Kingston ON Canada

-----Original Message-----
From: zhyjiang2006 at hotmail.com
Sent: Fri, 5 Oct 2012 05:41:29 +0800
To: jrkrideau at inbox.com
Subject: RE: [R] smoothScatter plot

 Hi John,

Thanks for your email. Your way works good. 

However, I was wondering if you can help with a smoothed scatter plot that has shadows with different darker blue color representing higher density of points.

Zhengyu 

Do you mean something like what is being discussed here? http://andrewgelman.com/2012/08/graphs-showing-uncertainty-using-lighter-intensities-for-the-lines-that-go-further-from-the-center-to-de-emphasize-the-edges/ 

If so I think there has been some discussion and accompanying ggplot2 code on google groups ggplot2 site.  

Otherwise can you explain a bit more clearly?

Date: Thu, 4 Oct 2012 05:46:46 -0800
From: jrkrideau at inbox.com
Subject: RE: [R] smoothScatter plot
To: zhyjiang2006 at hotmail.com
CC: r-help at r-project.org

Hi,

Do you mean something like this?  
=============================================
    scatter.smooth(x,y)scatter.smooth(x,y)
=============================================

It looks like invoking that dcols <- densCols(x,y) is callling in some package that is masking the basic::smoothScatter()  and applying some other version of smoothScatter, but I am not expert enough to be sure.

Another way to get the same result as mine with smoothScatter is to use the ggplot2 package.  it looks a bit more complicated but it is very good and in some ways easier to see exactly what is happening.

To try it you would need to install the ggplot2 package (install.packages("ggplot2")  then with your original x and y data frames
===============================================
library(ggplot2)
xy  <-  cbind(x, y)
names(xy)  <-  c("xx", "yy")

p  <-  ggplot(xy , aes(xx, yy )) + geom_point( ) + 
                             geom_smooth( method="loess", se =FALSE)
p 
================================================

Thanks for the data set.  However it really is easier to use dput()

To use dput() simply issue the command dput(myfile) where myfile is the file you are working with.  It will give you something like this:
======================================================
1> dput(x)
structure(c(0.4543462924, 0.2671718761, 0.1641577016, 1.1593356462, 
0.0421177346, 0.3127782861, 0.4515537795, 0.5332559665, 0.0913911528, 
0.1472054054, 0.1340672893, 1.2599304224, 0.3872026125, 0.0368560053, 
0.0371828779, 0.3999714282, 0.0175815783, 0.8871547761, 0.2706762487, 
0.7401904063, 0.0991320236, 0.2565567348, 0.5854167363, 0.7515717421, 
0.7220388222, 1.3528297744, 0.9339971349, 0.0128652431, 0.4102527051
), .Dim = c(29L, 1L), .Dimnames = list(NULL, "V1"))

1> dput(y)
structure(list(V1 = c(0.8669898448, 0.6698647266, 0.1641577016, 
0.4779091929, 0.2109900366, 0.2915241414, 0.2363116664, 0.3808731568, 
0.379908928, 0.2565868263, 0.1986675964, 0.7589866876, 0.6496236922, 
0.1327986663, 0.4196107999, 0.3436442638, 0.1910728051, 0.5625817464, 
0.1429791079, 0.6441837334, 0.1477153617, 0.369079266, 0.3839842979, 
0.39044223, 0.4186374286, 0.7611640016, 0.446291999, 0.2943343355, 
0.3019098386)), .Names = "V1", class = "data.frame", row.names = c(NA, 
-29L))
1> 

=======================================================

That is your x in dput() form.  You just copy it from the R terminal and paste it into your email message.  It is handy if you add the x  <-  and y  <-  to the output.  

Your method works just fine but it's a bit more cumbersome with a lot of data.

Also, please reply to the R-help list as well.  It is a source of much more expertise than me and it also can reply when a single person is unavailable.

I hope this helps

John Kane
Kingston ON Canada

-----Original Message-----
From: zhyjiang2006 at hotmail.com
Sent: Thu, 4 Oct 2012 05:19:14 +0800
To: jrkrideau at inbox.com
Subject: RE: [R] smoothScatter plot

 Hi John,

Thanks for your reply. But I cannot figure out how to use dput(). I included data and code below. Is that possible to make a plot similar to attached smoothing effect.

Zhengyu
###########

x<-read.table(text="0.4543462924
0.2671718761
0.1641577016
1.1593356462
0.0421177346
0.3127782861
0.4515537795
0.5332559665
0.0913911528
0.1472054054
0.1340672893
1.2599304224
0.3872026125
0.0368560053
0.0371828779
0.3999714282
0.0175815783
0.8871547761
0.2706762487
0.7401904063
0.0991320236
0.2565567348
0.5854167363
0.7515717421
0.7220388222
1.3528297744
0.9339971349
0.0128652431
0.4102527051",header=FALSE)
y<-read.table(text="0.8669898448
0.6698647266
0.1641577016
0.4779091929
0.2109900366
0.2915241414
0.2363116664
0.3808731568
0.379908928
0.2565868263
0.1986675964
0.7589866876
0.6496236922
0.1327986663
0.4196107999
0.3436442638
0.1910728051
0.5625817464
0.1429791079
0.6441837334
0.1477153617
0.369079266
0.3839842979
0.39044223
0.4186374286
0.7611640016
0.446291999
0.2943343355
0.3019098386",header=FALSE)
x<-data.matrix(x)
y<-data.matrix(y)
dcols <- densCols(x,y)
smoothScatter(x,y, col = dcols, pch=20,xlab="A",ylab="B")

 ################################

> Date: Tue, 2 Oct 2012 05:19:27 -0800
> From: jrkrideau at inbox.com
> Subject: RE: [R] smoothScatter plot
> To: zhyjiang2006 at hotmail.com; r-help at r-project.org
> 
> It's hard to know what's wrong with your code since you did not supply it. 
> 
> Please supply a small working example and some data. To supply data use the dput() function, see ?dput() for details. 
> 
> John Kane
> Kingston ON Canada
> 
> 
> > -----Original Message-----
> > From: zhyjiang2006 at hotmail.com
> > Sent: Tue, 2 Oct 2012 11:38:31 +0800
> > To: r-help at r-project.org
> > Subject: [R] smoothScatter plot
> > 
> > 
> > 
> > 
> > 
> > Hi, I want to make a plot similar to sm1 (attached). The code I tried is:
> > dcols <- densCols(x,y)
> > smoothScatter(x,y, col = dcols, pch=20,xlab="A",ylab="B")
> > abline(h=0, col="red")
> > But it turned out to be s1 (attached) with big dots. I was wondering if
> > anything wrong with my code. Thanks,Zhengyu
> > ______________________________________________
> > 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.
> 
> ____________________________________________________________
> FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family!
> Visit http://www.inbox.com/photosharing to find out more!
> 
> 

 Free Online Photosharing - Share your photos online with your friends and family!
 Visit http://www.inbox.com/photosharing [http://www.inbox.com/photosharing] to find out more!

  [http://www.inbox.com/marineaquarium] 
Free 3D Marine Aquarium Screensaver
 Watch dolphins, sharks & orcas on your desktop! Check it out at www.inbox.com/marineaquarium [http://www.inbox.com/marineaquarium]

____________________________________________________________
FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!




More information about the R-help mailing list