0

I use lattice package to draw the picture using the below csv file.

csv file

I used the below code and got the expected picture.

library(lattice)

data_1 <- read.table("./stackoverflow.csv", header=T, sep=",")
data <- na.omit(data_1)

xyplot(a ~ b | c, data = data, panel = function(x, y){
  panel.xyplot(x, y)
}
)

enter image description here

Moreover, I used the below code and got the next expected picture.

library(lattice)

data_1 <- read.table("./stackoverflow.csv", header=T, sep=",")
data <- na.omit(data_1)

xyplot(a ~ b | c, data = data, panel = function(x, y){
  panel.xyplot(x, y)
panel.lmline(x, y)
}
)

enter image description here

I want not to use panel.lmline for the data leading to the error message, and I want the below picture. The picture is the composite (damy) image.

enter image description here

How should I do to use the conditional branches for R lattice package.

R 4.0.5, lattice 0.20-38, MacOS 10.14.5 were used.

rrkk
  • 437
  • 1
  • 5
  • 15
  • 1
    The link you provided to data is not accessible without permissions. Sample data should not be stored on an external site. See [how to create a reproducible format](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for suggestions on including data in the question itself. – MrFlick Jul 02 '21 at 03:44
  • I have to say sorry for my mistake. I've changed the privacy settings of the csv file. I would appreciate it if you check it again. – rrkk Jul 02 '21 at 03:48
  • Just having the data on a different site is not good. Links rot over time. The question should be self-contained. – MrFlick Jul 02 '21 at 03:54
  • I apologize again. I've inserted another link. – rrkk Jul 02 '21 at 04:03

1 Answers1

1

The error is caused because all the values on the x-axis for that panel are the same so it's not possible to calculate a line. You can check for a non-zero range and only draw the line when possible using the following code

xyplot(a ~ b | c, data = data, panel = function(x, y){
  panel.xyplot(x, y)
  if(diff(range(x)) > 0) {
    panel.lmline(x, y)
  }
})
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Thank you for your quick response. Your code worked in my environment. Thank you again. – rrkk Jul 02 '21 at 04:13