# dots are shifted

#### Yankel

##### New Member
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!

#### Jake

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).

#### Yankel

##### New Member
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 ?

#### Yankel

##### New Member
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.

#### Jake

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).

#### Yankel

##### New Member
thanks, you have been a great help !

#### trinker

##### ggplot2orBust
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

#### Dason

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"))``````