简体   繁体   中英

Plotting vertical and horizontal lines at 0 using R's different graphing systems

I've checked for an answer to my problem, and the closest I could find was here: Why does plot behave differently for same but scaled data? . I understand atomic objects and already convert to a data frame.

I've loaded some chemical Reaction data:

   library(car)

    theURL <- "http://lib.stat.cmu.edu/datasets/Andrews/T30.1"
    theNames <- c("Table", "Number", "Row", "Experiment", "Temperature", 
    "Concentration", "Time", "Unchanged", "Converted", "Unwanted")
    Reaction <- read.table(theURL, header = F , col.names = theNames)
    Reaction <- Reaction[-c(1:4)]

I then draw a scatterplot; solid lines through the mean of X and the mean of Y; dotted lines at means ± 2SDs. Also a segment of slope Sy/Sx drawn at ± 5SDs because I couldn't get abline() to draw it.

    scatterplot(Reaction$Temperature, Reaction$Converted, smooth = FALSE, 
    regLine = FALSE, grid = FALSE, xlim = c(150, 185), xlab = "Temperature", ylab = "% Converted", main = "Reaction Results", ylim = c(45, 70))
    TempMean = mean(Reaction$Temperature)
    ConvMean = mean(Reaction$Converted)
    TempSD = sd(Reaction$Temperature)
    ConvSD = sd(Reaction$Converted)
    abline(col = c("red", "green"), v = TempMean, h = ConvMean)
    abline(col = "green", lty = "dotted", v = (c(TempMean - 2*TempSD, TempMean + 2*TempSD)))
    abline(col = "red", lty = 3, h = (c(ConvMean - 2*ConvSD, ConvMean + 2*ConvSD)))
    segments(TempMean - 5*TempSD, ConvMean - 5*ConvSD, TempMean + 5*TempSD, ConvMean + 5*ConvSD)

Bonferroni(?)限制双变量数据

...and now the big reveal. If I scale everything, the scatterplot essentially does the same thing.

    # Scale Reaction Data
    Reaction.scaled <- as.data.frame(scale(Reaction))
    # Mean and sd Lines
     scatterplot(Reaction.scaled$Temperature, Reaction.scaled$Converted, smooth = FALSE, regLine = FALSE, grid = FALSE, xlab = "Temperature", ylab = "% Converted", main = "Reaction Results")
    TempMean = mean(Reaction.scaled$Temperature)
    ConvMean = mean(Reaction.scaled$Converted)
    TempSD = sd(Reaction.scaled$Temperature)
    ConvSD = sd(Reaction.scaled$Converted)
    abline(col = c("red", "green"), v = TempMean, h = ConvMean)
    abline(col = "green", lty = "dotted", v = (c(TempMean - 2*TempSD, TempMean + 2*TempSD)))
    abline(col = "red", lty = 3, h = (c(ConvMean - 2*ConvSD, ConvMean + 2*ConvSD)))
    segments(TempMean - 5*TempSD, ConvMean - 5*ConvSD, TempMean + 5*TempSD, ConvMean + 5*ConvSD)

缩放图

...but the drawing doesn't show the scaled mean at (0,0). I suspect this is something to do with high-level vs low-level graphics functions.

As always, this problem was due to my own misunderstanding of the documentation.

Under ?scatterplot

reset.par
if TRUE (the default) then plotting parameters are reset to their previous values when scatterplot exits; if FALSE then the mar and mfcol parameters are altered for the current plotting device. Set to FALSE if you want to add graphical elements (such as lines) to the plot.

And, indeed, adding reset.par = FALSE to the car-based scatterplots above works. Try it yourself for fun at home!

The relevant graphic that caused the question, with the correction: 缩放数据用实线表示,虚线表示±2SDs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM