1. ## dots are shifted

Hello

I am trying to plot two scatter plots on one graph, for some reason, the second scatter is shifted...I don't know why, this is my data:

x
[1] 0.70 0.75 0.80 0.85 0.90 0.95 1.00 1.05 1.10 1.15 1.20 1.25 1.30
> y1
[1] 34 30 26 23 21 19 17 16 15 13 12 12 11
> y2
[1] 45 39 34 31 28 25 23 21 19 17 16 15 14
> y2-y1
[1] 11 9 8 8 7 6 6 5 4 4 4 3 3

this is my code:

Code:
``````plot(x,y1, axes=FALSE, col="blue", ylab="Sample size", xlab="Mean difference")
par(new=TRUE)
plot(x,y2 axes=FALSE, col="red", ylab="Sample size", xlab="Mean difference")``````
(x - mean differences y1 - sample size for 80% power y2 - sample size for 90% power)

what am I doing wrong ? According to this graph at some points for a power of 0.8 I need more subjects than for a power of 0.9!

2. ## Re: dots are shifted

You need to fix the range of the y-axis on each graph using the ylim parameter. By default plot() determines the range of the y-axis based on the range of the data, and since y1 and y2 have different ranges in this case, your two plot() calls are using different y-axis ranges (but the same x-axis range since both plots are plotting against the same x variable).

3. ## The Following User Says Thank You to Jake For This Useful Post:

Yankel (10-14-2012)

4. ## Re: dots are shifted

oh, I see what you mean...I didn't think about it.

without any relation, this is the identical graph using Stata

does anyone has an idea how I can create something like this in R, and when I say like this I also mean as beautiful as this ?

5. ## Re: dots are shifted

For both plots, set type="o", pch=20, col="red" or col="blue", and then you can add the legend using legend(). If you also want the gridlines you can add those using abline(), see the v and h parameters therein.

http://www.statmethods.net/graphs/line.html

6. ## Re: dots are shifted

thanks Jake

this is my code:

Code:
``````plot(x,y1, axes=FALSE, col="blue", ylab="Sample size", xlab="Mean difference")
axis(side = 2, at = seq(from=10, to=40, by=2))
axis(side = 1, at = c(0.7,0.8,0.9,1,1.1,1.2,1.3))
par(new=TRUE)
plot(x,y2, axes=FALSE, col="red", ylab="Sample size", xlab="Mean difference")
par(new=TRUE)
plot(x,y1, axes=FALSE, col="blue", ylab="Sample size", xlab="Mean difference", type="l")
par(new=TRUE)
plot(x,y2, axes=FALSE, col="red", ylab="Sample size", xlab="Mean difference", type="l")
grid()
legend("topright", c("0.8","0.9"), col = c("blue", "red"), lwd = 1, title="Power")``````
how do I change the ylim like you said ? I tried ylim=seq(from=10, to=40, by=2) but got an error.

7. ## Re: dots are shifted

ylim takes a vector of length two, where the two elements specify the highest and lowest tick points on the axis (in either order). You don't have to specify all of the intervening tick points. Although for future reference, when you are interested in specifying each tick point, you can use the yaxp and xaxp parameters (see ?par for more info).

8. ## The Following User Says Thank You to Jake For This Useful Post:

Yankel (10-14-2012)

9. ## Re: dots are shifted

thanks, you have been a great help !

10. ## Re: dots are shifted

If you're willing to put the time into ggplot it can provide a great deal of functionality and flexibility for some visuals. It does require learning the system (The Grammar of Graphics) but once you do you can make high quality graphics rather quickly. Here's your example:

Code:
``````dat <- structure(list(x = c(0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1, 1.05,
1.1, 1.15, 1.2, 1.25, 1.3), y1 = c(34, 30, 26, 23, 21, 19, 17,
16, 15, 13, 12, 12, 11), y2 = c(45, 39, 34, 31, 28, 25, 23, 21,
19, 17, 16, 15, 14)), .Names = c("x", "y1", "y2"), row.names = c(NA,
-13L), class = "data.frame")

library(reshape2); library(ggplot2)
dat2 <- melt(dat, id='x')

ggplot(data=dat2, aes(x=x, y=value, color=variable)) +
geom_point(size=3) + geom_line(size=.5) +
scale_y_continuous(breaks=seq(0, 45, by=5)) +
scale_x_continuous(breaks=seq(.7, 1.3, by=.05)) +
theme_bw() + ylab("Sample Size") + xlab("Mean Difference") +
theme(legend.position="bottom", legend.title=element_blank(),
legend.key = element_rect(colour = 'white'),
legend.background = element_rect(colour = "black"),
panel.border = element_blank(), axis.line = element_line(),
plot.background = element_rect(fill="lightcyan2")) +
ggtitle("Sample Size vs. Mean Difference by Power") +
scale_color_discrete(breaks=c("y1", "y2"),
labels=c("power=0.8", "power=0.9"))``````

EDIT: Made just the x and y axis visible not box around plot (compliments of Dason)
EDIT2: Per Dason's suggestion I changed the legend text

11. ## Re: dots are shifted

Couldn't hurt to modify the legend text either
Code:
``````ggplot(data=dat2, aes(x=x, y=value, color=variable)) +
geom_point(size=3) + geom_line(size=1.5) +
scale_y_continuous(breaks=seq(0, 45, by=5)) +
scale_x_continuous(breaks=seq(.7, 1.3, by=.05)) +
theme_bw() + ylab("Sample Size") + xlab("Mean Difference") +
theme(legend.position="bottom", legend.title=element_blank(),
legend.key = element_rect(colour = 'white'),
legend.background = element_rect(colour = "black")) +
ggtitle("Sample Size vs. Mean Difference by Power") +
theme(panel.border = element_blank(), axis.line = element_line()) +
scale_color_discrete(breaks=c("y1", "y2"),
labels=c("power=0.8", "power=0.9"))``````

12. ## The Following User Says Thank You to Dason For This Useful Post:

trinker (10-14-2012)

 Tweet

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts