在钙属性的最大值(ind=19,value=290)或通常使用 R 的 df fastfood of library(openintro) 中的任何异常值周围画一个红色圆圈

问题描述 投票:0回答:1

我最近开始了我在 generakl 的 R 和数据科学之旅

到目前为止我已经写了这段代码

`install.packages("openintro")
library(openintro)
install.packages("ggplot2")
library(ggplot2)

#load data 
data(fastfood)
#Find calcium's max value and indice
max.calcium <- max(na.omit(fastfood$calcium))
max.calcium
indices <- which(fastfood$calcium== max.calcium, arr.ind = TRUE)
indices

ggplot(fastfood, aes(x=seq_along(fastfood$calcium),y =calcium)) +
  geom_point() +
 xlab("Index")+  ylab("Calcium") +
  ggtitle("Scatterplot of X vs Y with Outliers Highlighted") +
    theme_bw()``

我如何使用 ggplot() 来标记这个属性“钙”的最大值点,周围有一个红色圆圈?

这个我已经写过了

r outliers
1个回答
0
投票

您可以在

x = ind, y = max.calcium
处添加一个点。这可以通过另一个
geom_point
层添加。使用大点(比如,
size = 5
),将其设为红色,并将其设为空心圆形(
shape = 21, fill = NA
):

ggplot(fastfood, aes(x = seq_along(calcium), y = calcium)) +
  geom_point() +
  geom_point(aes(x = indices, y = max.calcium),
             color = 'red', size = 5, shape = 21, fill = NA) +
  xlab("Index") +  
  ylab("Calcium") +
  ggtitle("Scatterplot of X vs Y with Outliers Highlighted") +
  theme_bw()

© www.soinside.com 2019 - 2024. All rights reserved.