如何根据数据属性的平均值在xyplot中添加一行?

时间:2019-05-04 23:55:50

标签: r lattice

我已经创建了想要得到的基本图形,只是无法弄清楚如何根据USArrests数据集中的谋杀属性的平均值在图形中添加一条线。之后,我还需要根据状态名称的高低来给它们上色。

我拥有的图表:https://ibb.co/V3VkYt4

我需要的图形:https://ibb.co/4TTnQM1

我尝试添加带有Murder属性的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)
}   

1 个答案:

答案 0 :(得分:1)

您的图形似乎显示了一条倾斜的回归线,而不是水平线。莱迪思可以直接从xyplot的变量或panel.lmline的回归模型(或常数)在panel.abline中添加回归线。需要更多的工作来对高于或低于选定谋杀率的州进行分类。这是一种使用显示两种回归线类型的格子进行处理的方法。

# 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])
    }
  )