R插入符号随机森林ggplot修改图例

R caret random forests ggplot modify legend

如何修改通过将 ggplot 应用于使用游侠算法构建的插入符号 object 生成的默认绘图图例?例如,假设我希望图例标题为“Splitting algo”而不是默认的“Splitting Rule”。

library(caret)

trainIndex <- createDataPartition(iris$Species, p = 0.80, list = FALSE)

train <- iris[ trainIndex,]

test  <- iris[-trainIndex,]

rfs <- train(Species ~ ., data = train, trControl = trainControl(method = "cv", number = 10), method = "ranger", tuneGrid = expand.grid(mtry = seq(1, 3, 1), splitrule = c("gini", "extratrees"), min.node.size = 1))

ggplot(rfs) + 
  xlab("Number of parameters") +
  ylab("Accuracy") +
  theme(legend.title = "Splitting algo")

您使用 scale_color_discretescale_shape_discrete 设置了图例的 name

library(ggplot2)

ggplot(rfs) + 
  xlab("Number of parameters") +
  ylab("Accuracy") +
  scale_color_discrete(name = "Splitting algo")  +
  scale_shape_discrete(name = "Splitting algo")