r - How to put the actual data points on the contour plot with ggplot? -
the code below produces contour plot using ggplot.
xgrid <- seq(min(mtcars$wt), max(mtcars$wt), 0.3) ygrid <- seq(min(mtcars$hp), max(mtcars$hp), 0.3) data.fit <- expand.grid(wt = xgrid, hp = ygrid) data.loess <- loess(qsec ~ wt * hp, data = mtcars) mtrx3d <- predict(data.loess, newdata = data.fit) mtrx3d[1:4, 1:4] require(reshape) mtrx.melt <- melt(mtrx3d, id.vars = c("wt", "hp"), measure.vars = "qsec") names(mtrx.melt) <- c("wt", "hp", "qsec") require(stringr) mtrx.melt$wt <- as.numeric(str_sub(mtrx.melt$wt, str_locate(mtrx.melt$wt, "=")[1,1] + 1)) mtrx.melt$hp <- as.numeric(str_sub(mtrx.melt$hp, str_locate(mtrx.melt$hp, "=")[1,1] + 1)) ggplot(mtrx.melt, aes(x = wt, y = hp, z = qsec)) + stat_contour()
i wonder how show acutal data points contour lines. have tried
ggplot(mtrx.melt, aes(x = wt, y = hp, z = qsec)) + stat_contour() + geom_point(mtrx.melt, aes(x = wt, y = hp, z = qsec))
but returned error message said
error: ggplot2 doesn't know how deal data of class uneval
this similar question how-can-i-overlay-points-and-lines-onto-a-contour-plot-with-ggplot2 based on different situation. in question, op wants annotate contour plot particular points wants highlight (where these points stored in different data set). case highlight original data points in mtrx.melt
. tried method in question failed work.
the plot above not based on dataset, expect have, want have contour plot original data points.
there several issues prevent produce desired result. of mentioned in comments.
however, op wants show actual data points trying plot regular grid mtrx.melt
used interpolate data, instead.
the follwing code
library(ggplot2) ggplot(mtrx.melt, aes(x = wt, y = hp, z = qsec))+ geom_point(aes(colour = qsec), mtcars) + stat_contour(aes(colour = ..level..))
creates
which op looking for.
please, note
- the contour levels color coded now.
- the actual data points
mtcars
data set shown. - the order of parameters in calls
geom_point()
, differentggplot
. geoms , stats expect aestheticmapping
first parameter ,data
second. geom_point()
called beforestat_contour()
ensure legend titleqsec
instead oflevel
. beware when replacingstat_contour()
geom_tile()
. tiles have plotted first layer , points on top.
edit in response this comment op, code have been written
ggplot(mapping = aes(x = wt, y = hp, z = qsec)) + geom_point(aes(colour = qsec), mtcars) + geom_contour(aes(colour = ..level..), mtrx.melt)
this makes more explicit 2 layers, i.e., geom_point()
, geom_contour()
, being used display data of 2 different data sources. aesthetics shared between both layers placed in initial ggplot()
call (which possible here because mtcars
, mtrx.melt
use same column names). colour
aesthetic different, each layer uses own definition.
in call ggplot()
, it's required specify parameter name mapping = aes(...)
because ggplot()
expects data
first parameter if unnamed.
geom_contour()
used here code appears more consistent , seems preferred on stat_contour()
following examples on ggplot2
documentation site.
Comments
Post a Comment