r - 将id标签添加到ggplot中限制线以上的点
问题描述
我有一个这样的数据框
id <- c(5738180,51845,167774,517814,1344920)
amount <- c(3.76765976,0.85195407,1.96821355,0.01464609,0.57378284)
outlier <- c("TRUE","FALSE","FALSE","FALSE","FALSE")
df.sample <- data.frame(id,amount,outlier)
我正在尝试绘制这些点并将id
标签添加到任何超出限制的点。在这种情况下(id=5738180)
我正在这样策划
library(tidyverse)
library(ggplot2)
library(ggrepel) # help avoid overlapping text labels
library(gridExtra) # adds custom table inside ggplot
library(scales) # breaks and labels for axes and legends
library(ggthemes) # Adding desired ggplot themes
df.sample %>%
ggplot(aes(x = as.numeric(row.names(df.sample)),
y = amount, label = as.character(id))) +
geom_point(alpha = 0.6, position = position_jitter(w = 0.05, h = 0.0),
aes(colour = (amount < 3)), size = 2) +
geom_hline(aes(yintercept = 3, linetype = "Limit"),
color = 'black', size = 1) +
geom_text(aes(y = 3,x = amount[4],
label = paste("Limit = ", round(3, 3)),
hjust = 0, vjust = 1.5)) +
geom_text_repel(data = subset(df.sample, outlier == 'TRUE'),
nudge_y = 0.75,
size = 4,
box.padding = 1.5,
point.padding = 0.5,
force = 100,
segment.size = 0.2,
segment.color = "grey50",
direction = "x") +
geom_label_repel(data = subset(df.sample, outlier == 'TRUE'),
nudge_y = 0.75,
size = 4,
box.padding = 0.5,
point.padding = 0.5,
force = 100,
segment.size = 0.2,
segment.color = "grey50",
direction = "x")
labs(title = "Outlier Detection",
y = "amount",
x = "") +
theme_few() +
theme(legend.position = "none",
axis.text = element_text(size = 10, face = "bold"),
axis.title = element_text(size = 10, face = "bold"),
plot.title = element_text(colour = "blue", hjust = 0.5,
size = 15, face = "bold"),
strip.text = element_text(size = 10, face = "bold")) +
scale_colour_manual(values = c("TRUE" = "green","FALSE" = "red"))
我遇到了一个错误"Error: Aesthetics must be either length 1 or the same as the data (1): x"
有人可以指出我正确的方向吗?
解决方案
问题在于geom_text_repel()
和geom_label_repel()
。您对数据进行了子集化,现在仅包含 1 行,但aes()
它们继承自具有 5 行的原始数据,因此出现错误。要解决此问题,请在调用之外对数据进行子集化ggplot()
,并为其更改美学。您还缺少一个+
aftergeom_label_repel()
并且下面的结果修改了nudge_y
tonudge_x
并删除了geom_text_repel()
.
outliers <- subset(df.sample, outlier == TRUE)
ggplot(data = df.sample,
aes(x = as.numeric(row.names(df.sample)),
y = amount,
label = as.character(id))) +
geom_point(alpha = 0.6,
position = position_jitter(w = 0.05, h = 0.0),
aes(colour = (amount < 3)),
size = 2) +
geom_hline(aes(yintercept = 3,
linetype = "Limit"),
color = 'black',
size = 1) +
geom_text(aes(y = 3,x = amount[4],
label = paste("Limit = ",
round(3, 3)),
hjust = 0,
vjust = 1.5)) +
geom_label_repel(data = outliers,
aes(x = as.numeric(rownames(outliers)),
y = amount,
label = amount),
nudge_x = 0.75,
size = 4,
box.padding = 0.5,
point.padding = 0.5,
force = 100,
segment.size = 0.2,
segment.color = "grey50",
direction = "x",
inherit.aes = F) +
labs(title = "Outlier Detection",
y = "amount",
x = "") +
theme_few() +
theme(legend.position = "none",
axis.text = element_text(size = 10, face = "bold"),
axis.title = element_text(size = 10, face = "bold"),
plot.title = element_text(colour = "blue", hjust = 0.5,
size = 15, face = "bold"),
strip.text = element_text(size = 10, face = "bold")) +
scale_colour_manual(values = c("TRUE" = "green","FALSE" = "red"))
推荐阅读
- javascript - 在本地设置 react CSS 加载器,但出现错误
- r - 嵌套在 case_when 中的 if 语句
- api-platform.com - 带有 api 平台的六边形架构
- r - 使用 R 自动重新分类插入到大 SpatialPolygons 中的小 SpatialPolygons
- scala - 散列火花数据框的多列
- php - 使用 Laravel 返回一对多 Eloquent Relation 中的最后一条记录
- http - 在真实网络中,服务器是否相互发送请求?
- elasticsearch - 如何根据 Elasticsearch 中的最大单词数对句子进行标记?
- reactjs - 为什么酶测试在 React 中不起作用?
- sql - 如何知道外键是否在删除子句上有级联