我最近开始了我在 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() 来标记这个属性“钙”的最大值点,周围有一个红色圆圈?
这个我已经写过了
您可以在
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()