R 具有不同颜色的异常值和geom文本编号标签的散点图
我有一个多栏的数据框。这里有一个例子R 具有不同颜色的异常值和geom文本编号标签的散点图,r,dataframe,ggplot2,colors,geom-text,R,Dataframe,Ggplot2,Colors,Geom Text,我有一个多栏的数据框。这里有一个例子 my_df <- data.frame(x = 1:5, y = c(50, 22, 15, 33, 49)) colnames(my_df) <- c("ID", "values") my_df 如果有人能帮忙,我会非常感激,因为我已经为此奋斗了一个多星期了 编辑:下面提供的答案回答了我的问题。这是我生成的绘图的代码,包括图例标题和每个变量的名称,供以后查找时参考 ggplot(my_df, aes(ID, values, color = f
my_df <- data.frame(x = 1:5, y = c(50, 22, 15, 33, 49))
colnames(my_df) <- c("ID", "values")
my_df
如果有人能帮忙,我会非常感激,因为我已经为此奋斗了一个多星期了
编辑:下面提供的答案回答了我的问题。这是我生成的绘图的代码,包括图例标题和每个变量的名称,供以后查找时参考
ggplot(my_df, aes(ID, values, color = factor(cut(values, c(0,24,48,Inf))))) +
geom_point(size=3) +
geom_text_repel(data = . %>% filter(values> 48), aes(label = values), show.legend = F)+
geom_text_repel(data = . %>% filter(values< 24), aes(label = values), show.legend = F)+
labs(title = "Beautiful Scatterplot", x = "ID", y = "Values", color = "Legend Title") +
scale_color_manual(labels = c("Below 24", "Between 24 and 48", "Above 48"), values = c("blue", "red", "purple"))
ggplot(my_df,aes(ID,value,color=因子(cut,value,c(0,24,48,Inf'))))+
几何点(尺寸=3)+
几何图形文本排斥(数据=.%>%过滤器(值>48)、aes(标签=值)、show.legend=F)+
几何图形文本排斥(数据=.%>%过滤器(值<24)、aes(标签=值)、show.legend=F)+
实验室(title=“漂亮的散点图”,x=“ID”,y=“值”,color=“图例标题”)+
比例颜色手册(标签=c(“低于24”、“介于24和48之间”、“高于48”),数值=c(“蓝色”、“红色”、“紫色”))
你可以试试
library(tidyverse)
library(ggrepel)
my_df %>%
mutate(col=case_when(values > 48 ~ 4,
values < 24 ~ 2,
T ~ 1)) %>%
ggplot(aes(ID, values, color = factor(col))) +
geom_point(size=3) +
geom_text_repel(data = . %>% filter(values> 48), aes(label = values)) +
scale_color_identity()
在回答中,在
aes()
中采用错误的语法,例如y=my_df$values
,这不是一个好主意。这非常有用-谢谢!非常感谢你,这正是我想要的!如果你对此感到满意,请考虑接受这个答案。
ggplot(data=my_df,aes(x=ID,y=values,label=ifelse(values>48,values,"")))+
geom_point(size=4,color = ifelse(my_df$values > 48, "red", "blue"))+
geom_text(vjust = 1.3,nudge_x = 0.15,aes(colour="red"),fontface = "bold",show.legend=F)
library(tidyverse)
library(ggrepel)
my_df %>%
mutate(col=case_when(values > 48 ~ 4,
values < 24 ~ 2,
T ~ 1)) %>%
ggplot(aes(ID, values, color = factor(col))) +
geom_point(size=3) +
geom_text_repel(data = . %>% filter(values> 48), aes(label = values)) +
scale_color_identity()
ggplot(my_df, aes(ID, values, color = factor(cut(values, c(0,24,48,Inf))))) +
geom_point(size=3) +
geom_text_repel(data = . %>% filter(values> 48), aes(label = values), show.legend = F)