Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何根据数据属性的平均值向xyplot添加线条?_R_Lattice - Fatal编程技术网

R 如何根据数据属性的平均值向xyplot添加线条?

R 如何根据数据属性的平均值向xyplot添加线条?,r,lattice,R,Lattice,我已经创建了我想要得到的基本图,我只是不知道如何根据USARREST数据集中谋杀属性的平均值向图中添加一条线。在那之后,我还需要根据州名称是否位于行上方或下方来为其上色 我的图表是: 我需要的图表: 我已经尝试添加一条带有谋杀属性的abline作为输入,该行出现在我的图形之外,不确定我做错了什么 library(lattice) textPlot <- function() { data <- cbind(rownames(USArrests), USArrests) nam

我已经创建了我想要得到的基本图,我只是不知道如何根据USARREST数据集中谋杀属性的平均值向图中添加一条线。在那之后,我还需要根据州名称是否位于行上方或下方来为其上色

我的图表是:

我需要的图表:

我已经尝试添加一条带有谋杀属性的abline作为输入,该行出现在我的图形之外,不确定我做错了什么

library(lattice)
textPlot <- function()
{
  data <- cbind(rownames(USArrests), USArrests)
  names(data) <- c("State", names(data)[2:5])

averageM <- mean(USArrests$Murder)

         xyplot(Murder~UrbanPop, data, 
         groups=State, panel=drawText, 
         main="Murder vs. Urban Population")

}

drawText <- function(x,y,groups,...)
  {
    panel.text(x=x,y=y,label=groups,cex=y/10)
}   

图形显示的是倾斜的回归线,而不是平均值的水平线。Lattice可以通过panel.lmline直接从变量或通过panel.abline从回归模型或常数在xyplot中添加回归线。需要做更多的工作来分类高于或低于选定谋杀率的州。这里有一种方法可以通过显示两种类型回归线的晶格来实现

# Load the lattice package, create data.frame with state names from USAarrests
  library(lattice)
  df <- data.frame(State = rownames(USArrests), USArrests)

# Determine regression and mean murder rate outside of xyplot()
# However, these operations don't have to be done outside of the lattice function
  fm <- lm(Murder ~ UrbanPop, df)
  averageM <- mean(USArrests$Murder)

# Add a variable to the data.frame indicating the classification
  df$type <- factor(ifelse(df$Murder < fm$fitted, "low", "high"))

# Plot via lattice with explicit panel() function
  xyplot(Murder ~ UrbanPop, data = df,
    panel = function(x, y, ...) {
      panel.abline(fm, col = "red", lwd = 2)
#     panel.lmline(x, y, col = "red", lwd = 2) # This would do the same
      panel.abline(h = averageM, col = "red", lty = 2, lwd = 2)
#     panel.abline(h = mean(y), col = "red", lty = 2, lwd = 2) # This would do the same
      panel.text(x, y, labels = df$State, cex = y/10, col = c(2,4)[df$type])
    }
  )

也许是abline?abline是用于基本R图形,而不是晶格图。这很有效!非常感谢你,我非常感谢你在可选的方式添加做这行。