ggplot2 - R highlight a point on a line -
here code produces plot. 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 produced highlight point on line time = 88.
i want highlight point on the line time = 87.925
is possible? thing not have corresponding value time. there way find put point on lines time = 87.925 or interpolation need take place can a value time?
thank you!
you can use ggplot_build pull out interpolated value each line . . .
## create fake ggplot smooth values using linear fit ## tmp.plot <- ggplot(stacked, aes(x = time, y = value, colour = variable)) + stat_smooth(method="lm") ## use ggplot_build pull out smoothing values ## tmp.dat <- ggplot_build(tmp.plot)$data[[1]] ## find x values closest 87.925 each variable ## tmp.ids <- which(abs(tmp.dat$x - 87.925)<0.001) ## store x , y values each variable ## new.points <- tmp.dat[tmp.ids,2:3] ## create data frame 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 new points original data frame ## stacked <- rbind(stacked,newpts) ## plot ## myplot
Comments
Post a Comment