R高亮显示直线上的点

R高亮显示直线上的点,r,ggplot2,R,Ggplot2,这是我的代码,生成一个绘图。您可以运行它: library(ggplot2) library(grid) time <- c(87,87.5, 88,87,87.5,88) value <- c(10.25,10.12,9.9,8,7,6) variable <-c("a","a","a","b","b","b") PointSize <-c(5,5,5,5,5,5) ShapeType <-c(10,10,10,10,10,10) stacked <- d

这是我的代码,生成一个绘图。您可以运行它:

library(ggplot2)
library(grid)
time <- c(87,87.5, 88,87,87.5,88)
value <- c(10.25,10.12,9.9,8,7,6)
variable <-c("a","a","a","b","b","b")
PointSize <-c(5,5,5,5,5,5)
ShapeType <-c(10,10,10,10,10,10)

stacked <- data.frame(time, value, variable, PointSize, ShapeType)

stacked$PointSize <- ifelse(stacked$time==88, 8, 5)
stacked$ShapeType <- ifelse(stacked$time==88, 16,10)

MyPlot <- ggplot(stacked, aes(x=time, y=value, colour=variable, group=variable)) + geom_line() + xlab("Strike") + geom_point(aes(shape = ShapeType, size = PointSize)) + theme(axis.text.x = element_text(angle = 90, hjust = 1), axis.text = element_text(size = 10),   axis.title=element_text(size=14),  plot.title = element_text(size = rel(2)) ,  legend.position = "bottom", legend.text = element_text(size = 10), legend.key.size = unit(1, "cm") ) + scale_shape_identity(guide="none")+scale_size_identity(guide="none")

MyPlot
库(ggplot2)
图书馆(网格)

时间您可以使用ggplot\U build为每条线提取插值

## create a fake ggplot to smooth your values using a linear fit ##
tmp.plot <- ggplot(stacked, aes(x = time, y = value, colour = variable)) + stat_smooth(method="lm")

## use ggplot_build to pull out the smoothing values ##
tmp.dat <- ggplot_build(tmp.plot)$data[[1]]

## find the x values closest to 87.925 for each variable ##
tmp.ids <- which(abs(tmp.dat$x - 87.925)<0.001)

## store the x and y values for each variable ##
new.points <- tmp.dat[tmp.ids,2:3]

## create a data frame with the new points ##
newpts <- data.frame(new.points,c("a","b"),c(8,8),c(16,16))

names(newpts) <- c("time","value","variable","PointSize","ShapeType")

## add the new points to your original data frame ##
stacked <- rbind(stacked,newpts)

## plot ##
MyPlot
##创建一个伪ggplot,使用线性拟合平滑值##

tmp.plot除了使用点高亮显示87.925的时间值外,还可以使用垂直线:

ggplot(stacked, aes(x=time, y=value, colour=variable, group=variable)) +
  geom_line() +
  geom_point(aes(shape = ShapeType, size = PointSize)) +
  geom_vline(aes(xintercept=87.925)) +
  xlab("Strike") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1), axis.text = element_text(size = 10),
        axis.title=element_text(size=14), plot.title = element_text(size = rel(2)), legend.position = "bottom",
        legend.text = element_text(size = 10), legend.key.size = unit(1, "cm")) +
  scale_shape_identity(guide="none") +
  scale_size_identity(guide="none")
结果是:


更新:您可以使用
geom\u段添加短线。将
geom\u vline
替换为

geom_segment(aes(x = 87.925, y = 6, xend = 87.925, yend = 6.3), color="black") +
geom_segment(aes(x = 87.925, y = 9.8, xend = 87.925, yend = 10.05), color="black") +
其结果是:

尝试运行代码时,主题(axis.text.x=element\u text(角度=90,hjust=1),axis.text=element\u text(大小=10)中出现了
错误:找不到函数“unit”
@CCurtis try
库(网格)
然后再次运行它。这很有效。我看事情的方式有两个选项。在87.925处添加点,然后通过插值找到正确的y值,或者在87.925处向直线添加点,通过插值找到正确的y值,然后应用与在88处对点所做的处理相同的
ifelse
方法。不是ggplot用户,但这是我的方式我能不能让这条线只升到蓝线然后停下来?