【问题标题】:Draw rectangulars in a confusion matrix using geom_rect()使用 geom_rect() 在混淆矩阵中绘制矩形
【发布时间】:2021-07-21 02:08:04
【问题描述】:

我使用以下代码创建了一个混淆矩阵:

ggplot(data, aes(x = Predicted, y = Actual)) +
geom_tile(aes(fill = Percentage), alpha = .8) +
geom_text(aes(label = Percentage), color = "white") +
scale_fill_gradientn(colours = rev(brewer.pal(5, "Spectral")), limits = c(0, 100)) +
scale_x_discrete(position = "top")

我想画 95% 和 100% 左右的黑色矩​​形。

谢谢

【问题讨论】:

    标签: r ggplot2 matrix


    【解决方案1】:

    尝试在第一个geom_tile之后添加这一行

      geom_tile(aes(x = Predicted, y = Actual),
                data=data[data$Predicted == data$Actual, ], size=1, color="black", fill = 'transparent') +
    

    完整代码:

    ggplot(data, aes(x = Predicted, y = Actual)) +
      geom_tile(aes(fill = Percentage), alpha = .8) +
      geom_tile(aes(x = Predicted, y = Actual),
                data=data[data$Predicted == data$Actual, ], size=1, color="black", fill = 'transparent') +
      geom_text(aes(label = Percentage), color = "white") +
      scale_fill_gradientn(colours = rev(brewer.pal(5, "Spectral")), limits = c(0, 100)) +
      scale_x_discrete(position = "top")
    

    【讨论】:

      猜你喜欢
      • 2016-01-31
      • 2016-10-20
      • 1970-01-01
      • 1970-01-01
      • 2020-07-15
      • 2016-06-04
      • 1970-01-01
      • 2017-10-17
      相关资源
      最近更新 更多