Dear all
I'm realy new to R, so I hope you can help me, as I didn't find any
solution in the common books.
Since some days I'm trying to create the following plot: A scatterplott
showning two different groups side-by-side with according regression lines. Both
datasets only have the same five factors, so the scatters will form a kind of
column at each factor. When I use "scatterplot" (package
"car"), then I can plot two groups in the same graph by using the
command "groups", but the scatters of both groups are then plotted on
top of eachother using different symbols and they can hardly be distingushed.
How can I plot them side by side, so that the groups do not overlap? And how can
I give different colours to the groups and the according regression line?(This
is, what I got so far: http://img7.imageshack.us/img7/227/almostgood.jpg)
I tried to use the commands used in "boxplot", to solve this problem.
In this commant, it's possible to plot different datasets side-by-side by
defining the position of the bars (example: at = 1:5 - 0.4). A second
boxplot-chart can then be added by adding the command "add=TRUE" to
the line and defining another position. Both commands don't function within
the scatterplot-command.
By the way: It's realy necessary to plott the data as scatters and not as
boxplots. With the command "plot", I can not plot the data by groups
(I tried it with the commands "subset" and "groups", but
obviously, there is no way to do so).
I'm greatful for every (simple) solution
Thanks in advance
Karin Schneeberger
MSc-student
University of Berne
Switzerland
[[alternative HTML version deleted]]
Hi, You could do this very easily using ggplot2,> #install.packages("ggplot2", dep=TRUE)> library(ggplot2) > c <- ggplot(mtcars, aes(y=wt, x=mpg)) + facet_grid(. ~ cyl) > c + stat_smooth(method=lm) + geom_point()See more examples on Hadley's website: http://had.co.nz/ggplot2/ Hope this helps, baptiste On 26 Apr 2009, at 10:29, nonunah at yahoo.de wrote:> Dear all > > I'm realy new to R, so I hope you can help me, as I didn't find any > solution in the common books. > > Since some days I'm trying to create the following plot: A > scatterplott showning two different groups side-by-side with > according regression lines. Both datasets only have the same five > factors, so the scatters will form a kind of column at each factor. > When I use "scatterplot" (package "car"), then I can plot two groups > in the same graph by using the command "groups", but the scatters of > both groups are then plotted on top of eachother using different > symbols and they can hardly be distingushed. How can I plot them > side by side, so that the groups do not overlap? And how can I give > different colours to the groups and the according regression line? > (This is, what I got so far: http://img7.imageshack.us/img7/227/almostgood.jpg) > > I tried to use the commands used in "boxplot", to solve this > problem. In this commant, it's possible to plot different datasets > side-by-side by defining the position of the bars (example: at = 1:5 > - 0.4). A second boxplot-chart can then be added by adding the > command "add=TRUE" to the line and defining another position. Both > commands don't function within the scatterplot-command. > > By the way: It's realy necessary to plott the data as scatters and > not as boxplots. With the command "plot", I can not plot the data by > groups (I tried it with the commands "subset" and "groups", but > obviously, there is no way to do so). > > I'm greatful for every (simple) solution > Thanks in advance > > Karin Schneeberger > MSc-student > University of Berne > Switzerland > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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._____________________________ Baptiste Augui? School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag
On Sun, 26 Apr 2009 09:29:39 +0000 (GMT) "nonunah at yahoo.de"
<nonunah at yahoo.de> wrote:
ND> I'm realy new to R, so I hope you can help me, as I didn't find
any
ND> solution in the common books.
Have a look at the online resources at:
http://cran.r-project.org/other-docs.html
There is also stuff on graphics.
Furthermore the lattice package and book are highly recommended as well.
ND> By the way: It's realy necessary to plott the data as scatters and
ND> not as boxplots. With the command "plot", I can not plot the
data
ND> by groups (I tried it with the commands "subset" and
"groups", but
ND> obviously, there is no way to do so).
There is always a way. I just don't understand why it is necessary to
plot this as a scatterplot.
Look your "problem" is that your data have integer values. So it is
very clear that they will be overplotted and that the reader has no
idea at which point are many observations even when you split the data
on the x axis into groups. Or even if you make a per group plot as
Baptiste suggested and as would be possible with lattice as well.
I could offer an easy solution. You can split into groups manually by
changing your x values slightly groupwise. But still you dont see how
many data are on each point. You could add some noise with the jitter
function (see ?jitter ), so that one sees that there are many
observation at one point. However it introduces the appearence that you
dont deal with categorical data, which might not be intended...
daten<-data.frame(y=sample(c(1,2,3),24,replace=T),
x=rep(c(1,2),each=12),group=rep(c(1,2)))
daten
# plot with overplotting, no information gain
plot(daten$x,daten$y)
# plot with jitter
# prepare data
daten$x2<-ifelse(daten$group==1,daten$x-0.02,daten$x+0.02)
plot(c(0,2),c(0,4),type="n") # empty plot you could use real data
# plot points, see ?jitter for options
points(jitter(y)~x2,data=subset(daten,group==1),col="blue",pch=1)
points(jitter(y)~x2,data=subset(daten,group==2),col="red",pch=2)
# regression lines added:
abline(lm(y~x,data=subset(daten,group==1)),col="blue")
abline(lm(y~x,data=subset(daten,group==2)),col="red")
legend("topleft",c("group 1","group 2",
"regression group 1","regression group 2") ,lty=c(0,0,1,1),
pch=c(1,2,NA,NA),
col=rep(c("blue","red"),2),bty="n")
But I believe there are better solutions. You should think about a
different plot like a ballon plot or so.
Then I doubt whether a linear regression is really good here since we
deal with categorical data...
ND> I'm greatful for every (simple) solution
Sorry if it is not simple. You see R has the advantage that it is
highly configurable. But you still need to know the message...
hth
Stefan
Dear Karin,
If I understand correctly what you want, the scatterplot function in the car
package isn't designed to produce it, but there are many ways to draw
side-by-side scatterplots. Here is one, using basic R graphics:
par(mfrow=c(1,2))
by(Data, Data$group,
function(x) {
plot(Pulls ~ Resistance, data=x, main=paste("group =",
group[1]))
abline(lm(Pulls ~ Resistance, data=x))
}
)
This assumes that your data are in a data frame named Data, with variables
group, Pulls, and Resistance.
I hope this helps,
John
------------------------------
John Fox, Professor
Department of Sociology
McMaster University
Hamilton, Ontario, Canada
web: socserv.mcmaster.ca/jfox
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at
r-project.org]
On> Behalf Of nonunah at yahoo.de
> Sent: April-26-09 5:30 AM
> To: r-help at r-project.org
> Subject: [R] Scatterplot of two groups side-by-side?
>
> Dear all
>
> I'm realy new to R, so I hope you can help me, as I didn't find any
solution> in the common books.
>
> Since some days I'm trying to create the following plot: A scatterplott
> showning two different groups side-by-side with according regression
lines.> Both datasets only have the same five factors, so the scatters will form a
> kind of column at each factor. When I use "scatterplot" (package
"car"),
then> I can plot two groups in the same graph by using the command
"groups", but
> the scatters of both groups are then plotted on top of eachother using
> different symbols and they can hardly be distingushed. How can I plot them
> side by side, so that the groups do not overlap? And how can I give
different> colours to the groups and the according regression line?(This is, what I
got> so far: http://img7.imageshack.us/img7/227/almostgood.jpg)
>
> I tried to use the commands used in "boxplot", to solve this
problem. In
this> commant, it's possible to plot different datasets side-by-side by
defining
> the position of the bars (example: at = 1:5 - 0.4). A second boxplot-chart
> can then be added by adding the command "add=TRUE" to the line
and
defining> another position. Both commands don't function within the scatterplot-
> command.
>
> By the way: It's realy necessary to plott the data as scatters and not
as
> boxplots. With the command "plot", I can not plot the data by
groups (I
tried> it with the commands "subset" and "groups", but
obviously, there is no way
to> do so).
>
> I'm greatful for every (simple) solution
> Thanks in advance
>
> Karin Schneeberger
> MSc-student
> University of Berne
> Switzerland
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
nonunah at yahoo.de wrote:> Dear all > > I'm realy new to R, so I hope you can help me, as I didn't find any solution in the common books. > > Since some days I'm trying to create the following plot: A scatterplott showning two different groups side-by-side with according regression lines. Both datasets only have the same five factors, so the scatters will form a kind of column at each factor. When I use "scatterplot" (package "car"), then I can plot two groups in the same graph by using the command "groups", but the scatters of both groups are then plotted on top of eachother using different symbols and they can hardly be distingushed. How can I plot them side by side, so that the groups do not overlap? And how can I give different colours to the groups and the according regression line?(This is, what I got so far: http://img7.imageshack.us/img7/227/almostgood.jpg) > > I tried to use the commands used in "boxplot", to solve this problem. In this commant, it's possible to plot different datasets side-by-side by defining the position of the bars (example: at = 1:5 - 0.4). A second boxplot-chart can then be added by adding the command "add=TRUE" to the line and defining another position. Both commands don't function within the scatterplot-command. > > By the way: It's realy necessary to plott the data as scatters and not as boxplots. With the command "plot", I can not plot the data by groups (I tried it with the commands "subset" and "groups", but obviously, there is no way to do so). > >Hi Karin, I would use cluster.overplot in the plotrix package for the scattergram. This will adjust the positions of the points for the two groups so that they form little clusters instead of overplotting. You can color the two sets of points differently as well as use different symbols. Then by adding the two regression lines in the same two colors, you should have an easier to interpret plot. Jim