+ Reply to Thread
Results 1 to 9 of 9

Thread: dots are shifted

  1. #1
    Points: 6,530, Level: 52
    Level completed: 90%, Points required for next Level: 20

    Posts
    128
    Thanks
    8
    Thanked 0 Times in 0 Posts

    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. #2
    Cookie Scientist
    Points: 9,364, Level: 65
    Level completed: 5%, Points required for next Level: 286
    Jake's Avatar
    Location
    Boulder, CO
    Posts
    1,141
    Thanks
    46
    Thanked 480 Times in 367 Posts

    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).
    “In God we trust. All others must bring data.”
    ~W. Edwards Deming

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

    Yankel (10-14-2012)

  4. #3
    Points: 6,530, Level: 52
    Level completed: 90%, Points required for next Level: 20

    Posts
    128
    Thanks
    8
    Thanked 0 Times in 0 Posts

    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 ?
    Attached Images  

  5. #4
    Cookie Scientist
    Points: 9,364, Level: 65
    Level completed: 5%, Points required for next Level: 286
    Jake's Avatar
    Location
    Boulder, CO
    Posts
    1,141
    Thanks
    46
    Thanked 480 Times in 367 Posts

    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.

    See also the useful pointers at these links:
    http://www.statmethods.net/graphs/line.html
    http://www.statmethods.net/advgraphs/axes.html
    “In God we trust. All others must bring data.”
    ~W. Edwards Deming

  6. #5
    Points: 6,530, Level: 52
    Level completed: 90%, Points required for next Level: 20

    Posts
    128
    Thanks
    8
    Thanked 0 Times in 0 Posts

    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. #6
    Cookie Scientist
    Points: 9,364, Level: 65
    Level completed: 5%, Points required for next Level: 286
    Jake's Avatar
    Location
    Boulder, CO
    Posts
    1,141
    Thanks
    46
    Thanked 480 Times in 367 Posts

    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).
    “In God we trust. All others must bring data.”
    ~W. Edwards Deming

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

    Yankel (10-14-2012)

  9. #7
    Points: 6,530, Level: 52
    Level completed: 90%, Points required for next Level: 20

    Posts
    128
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Re: dots are shifted

    thanks, you have been a great help !

  10. #8
    ggplot2orBust
    Points: 36,177, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    User with most referrers
    trinker's Avatar
    Location
    Buffalo, NY
    Posts
    3,995
    Thanks
    1,408
    Thanked 764 Times in 684 Posts

    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
    "If you torture the data long enough it will eventually confess."
    -Ronald Harry Coase -

  11. #9
    Beep
    Points: 63,267, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardCommunity AwardMaster TaggerFrequent Poster
    Dason's Avatar
    Location
    Ames, IA
    Posts
    11,315
    Thanks
    266
    Thanked 2,202 Times in 1,881 Posts

    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"))
    Morte a tutti i raptors
    001100010010011110100001101101110011

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

    trinker (10-14-2012)

+ Reply to Thread

           




Posting Permissions

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






Advertise on Talk Stats