简体   繁体   中英

How to add a red dot on the x-axis of a density plot with ggplot

I'm running some synthetic experiments.

I have 3 parameter distributions (m) and the true values of each parameter (trueValues).

library('reshape2')
library('ggplot2')

trueValues <- c("V1"=0,"V2"=2.5,"V3"=5)
set.seed(1)
m <- matrix(cbind("V1"=rnorm(5, 0), "V2"=rnorm(5, 2), "V3"=rnorm(5, 5)), nrow=5, ncol=3)
df <- melt(m)
ggplot(df, aes(x=value)) + geom_density() + facet_wrap(~Var2)

Now, how can I plot a red dot on the x-axis to show the true value?

在此处输入图片说明

You might try:

trueValues <- data.frame("Var2" = c(1, 2, 3), "value" = c(0, 2.5, 5))
ggplot(df, aes(x=value)) + geom_density() + facet_wrap(~Var2) + geom_point(data = trueValues, y = 0, color="red")

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM