Reputation: 9018
Here is my code that produces a plot. You can run it:
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
The plot that is produced highlight the point on the line where the time = 88.
I want to also highlight the point on the the line where the time = 87.925
Is this possible? The thing is that I do not have corresponding value for that time. IS there a way to just find put the point on the lines where time = 87.925 or does some interpolation need to take place so I can get a a value for that time?
Thank you!
Upvotes: 0
Views: 1420
Reputation: 83275
Instead of using a point for highlighting the 87.925 value for time, you can also use a vertical line:
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")
the result:
Update: you can add short lines with geom_segment
. Replace geom_vline
with
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") +
which results in:
Upvotes: 0
Reputation: 282
You can use ggplot_build to pull out an interpolated value for each line . . .
## 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
Upvotes: 2