0
votes

I am working with a data frame called d in R. I want to plot a scatter plot using two of the columns, include a best-fit regression line, and also plot binned means. I have calculated the centers of the bins and binned means, and included those as columns in the data frame. I can make the scatter plot and regression line work, but cannot get the binned means to show up. Using the code below I get no errors, but the panel.points function does not show up.

    scatter.Epsilon <- xyplot(Epsilon ~ data.subset.UpdatedVS30.091015,
                      data = d,
                      grid = TRUE,
                      scales = list(x = list(log = 10)),
                      xlab = "Vs30 (m/s)",
                      ylab = "Epsilon",
                      ylim = c(-4, 3),
                      xlim = c(10^2,10^3.4),
                      subscripts = TRUE,
                      panel=function(x,y,subscripts,...) {
                        panel.xyplot(x,y) 
                        panel.abline(mod <- lm(y ~ x), col = 'black')
                        panel.points(d$bin.ep[subscripts], d$means.ep[subscripts],
                                     col = 'red')})
                      scatter.Epsilon

A simplified data set would be:

    dist <- rnorm(10,4,100)
    x <- seq(1,100)
    bin <-rep(50,100)
    mean <- rep(mean(dist),100)
    d <- data.frame(x,dist,bin,mean)

where dist ~ x is the scatterplot component, and mean represents the binned mean for data points between 1-100, and bin is the bin's center (at 50). I want to add one point at (bin, mean) on top of dist ~ x. My real data set has multiple bins and means based on data.subset.UpdatedVS30.091015 that I want to add on top of Epsilon ~ data.subset.UpdatedVS30.091015.

1
Can you provide some code that will produce the dataframe d? or something with the same structure? Is data.subset a component of d or something else altogether? - atiretoo
I updated my original question with a simplified data frame - hopefully this helps. data.subset.UpdatedVS30.091015 is a component of d, the $ was a typo originally. Thank you. - GAP

1 Answers

0
votes

I think you might be trying to do too much work in the call to panel.points. Using your example data, this code works fine:

scatter.Epsilon <- xyplot(dist ~ x,
                      data = d,
                      grid = TRUE,
                      subscripts = TRUE,
                      panel=function(x,y,subscripts,...) {
                        panel.xyplot(x,y) 
                        panel.abline(mod <- lm(y ~ x), col = 'black')
                        panel.points(bin,mean,col = 'red')})

and plots a red point right where it should be. Have you tried just

panel.points(bin.ep,means.ep,col='red')

There is no grouping variable in your formula, so no need for subscripts.