这是我生成情节的代码。你可以运行它:
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
生成的绘图突出显示时间= 88的行上的点。
我还要强调时间= 87.925
的行上的点这可能吗?问题是我当时没有相应的价值。有没有办法找到时间= 87.925的线上的点或者是否需要进行一些插值,所以我可以得到一个该值的值?
谢谢!
答案 0 :(得分:2)
您可以使用ggplot_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
答案 1 :(得分:0)
您可以使用垂直线代替使用高亮显示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_segment
添加短行。将geom_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") +
导致: