copy from ggplot2
adapt stuff