Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7080869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:50:53+00:00 2026-05-28T06:50:53+00:00

I’m having a bit of a trouble here, please help me. I have this

  • 0

I’m having a bit of a trouble here, please help me.
I have this data

set.seed(4)
mydata <- data.frame(var = rnorm(100),
                     temp = rnorm(100),
                     subj = as.factor(rep(c(1:10),5)),
                     trt = rep(c("A","B"), 50))

and this model that fits them

lm  <- lm(var ~ temp * subj, data = mydata)

I want to plot the results with lattice and fit the regression line, predicted with my model, through them. To do so, I’m using this approach, outlined “Lattice Tricks for the power useR” by D. Sarkar

temp_rng <- range(mydata$temp, finite = TRUE)

grid <- expand.grid(temp = do.breaks(temp_rng, 30),
                    subj = unique(mydata$subj),
                    trt = unique(mydata$trt))

model <- cbind(grid, var = predict(lm, newdata = grid))

orig <- mydata[c("var","temp","subj","trt")]

combined <- make.groups(original = orig, model = model)


xyplot(var ~ temp | subj, 
       data = combined,
       groups = which,
       type = c("p", "l"),
       distribute.type = TRUE
       )

So far every thing is fine, but I also want to assign a fill color to the data points for the two treatments trt=1 and trt=2.

So I have written this piece of code, that works fine, but when it comes to plot the regression line, it seems that type is not recognized by the panel function…

my.fill <- c("black", "grey")

plot <- with(combined,
        xyplot(var ~ temp | subj,
              data = combined,
              group = combined$which,
              type = c("p", "l"),
              distribute.type = TRUE,
              panel = function(x, y, ..., subscripts){
                     fill <- my.fill[combined$trt[subscripts]] 
                     panel.xyplot(x, y, pch = 21, fill = my.fill, col = "black")
                     },
             key = list(space = "right",
                     text = list(c("trt1", "trt2"), cex = 0.8),
                     points = list(pch = c(21), fill = c("black", "grey")),
                     rep = FALSE)
                     )
      )
plot

I’ve also tried to move type and distribute type within panel.xyplot, as well as subsetting the data in it panel.xyplot like this

plot <- with(combined,
        xyplot(var ~ temp | subj,
              data = combined,
              panel = function(x, y, ..., subscripts){
                     fill <- my.fill[combined$trt[subscripts]] 
                     panel.xyplot(x[combined$which=="original"], y[combined$which=="original"], pch = 21, fill = my.fill, col = "black")
                     panel.xyplot(x[combined$which=="model"], y[combined$which=="model"], type = "l", col = "black")
                     },
             key = list(space = "right",
                     text = list(c("trt1", "trt2"), cex = 0.8),
                     points = list(pch = c(21), fill = c("black", "grey")),
                     rep = FALSE)
                     )
      )
plot

but no success with that either.

Can anyone help me to get the predicted values plotted as a line instead of being points?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:50:54+00:00Added an answer on May 28, 2026 at 6:50 am

    This might be a job for the latticeExtra package.

    library(latticeExtra)
    p1 <- xyplot(var ~ temp | subj, data=orig, panel=function(..., subscripts) {
      fill <- my.fill[combined$trt[subscripts]] 
      panel.xyplot(..., pch=21, fill=my.fill, col="black")
    })
    p2 <- xyplot(var ~ temp | subj, data=model, type="l")
    p1+p2
    

    enter image description here

    I’m not sure what’s going on in your first attempt, but the one with the subscripts isn’t working because x and y are a subset of the data for subj, so subsetting them using a vector based on combined won’t work the way you think it will. Try this instead.

    xyplot(var ~ temp | subj, groups=which, data = combined,
           panel = function(x, y, groups, subscripts){
             fill <- my.fill[combined$trt[subscripts]]
             g <- groups[subscripts]
             panel.points(x[g=="original"], y[g=="original"], pch = 21, 
                          fill = my.fill, col = "black")
             panel.lines(x[g=="model"], y[g=="model"], col = "black")
           },
           key = list(space = "right",
             text = list(c("trt1", "trt2"), cex = 0.8),
             points = list(pch = c(21), fill = c("black", "grey")),
             rep = FALSE)
           )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I want to construct a data frame in an Rcpp function, but when I
I'm having trouble keeping the paragraph square between the quote marks. In firefox the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.