I want to create a plot in ggplot for a box plot and a line plot in the same figure. I have a data frame which looks like the following:
Lambda | means | theorMean
1 0.1 10.07989 10
2 0.1 10.55681 10
3 0.1 10.26660 10
4 0.1 10.29234 10
5 0.1 10.07754 10
...
The means
are sample means and the theoretical means are theorMeans
. I want to plot the distribution of the sample means by box plots, while the theoretical means using a straight line.
This is what I have so far ...
library(ggplot2)
library(scales)
p <- ggplot(summ, aes(x=factor(Lambda), y=means)) +
geom_boxplot() +
geom_line(data=summ, aes(x=log10(Lambda), y=means))
The problem is that, for a box plot or a violin plot, I need to use the x axis as a factor. On the other hand, I need the x axis to be a number. I basically want to fit a theoretical line, to the box plots I generate. How can I possible do this?
aes(x=factor(Lambda), y=means)
fromggplot()
and place it inside ofgeom_boxplot()
. Alternatively, you could useinherit.aes = FALSE
to block the inheritance of theaes
ingeom_line
. Please let me know if I've misinterpreted something. – lnNoamthermion
in the data.frame. I can simply plot (lambda
vs.1/lambda
). Its just thatlambda
is originally log-scaled. So I can't line up the factors which are not log-scaled to a line. I tried your suggestion ofggplot(summ)+geom_boxplot(aes(x=factor(Lambda), y=means)) + geom_line(aes(x=Lambda, y=means))
and that doesn't help. Thanks for your suggestion though! – ssm