簡體   English   中英

如何使用ggplot在密度圖的x軸上添加紅點

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

我正在進行一些綜合實驗。

我有3個參數分布(m)和每個參數的真實值(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)

現在,如何在x軸上繪制紅點以顯示真實值?

在此處輸入圖片說明

您可以嘗試:

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")

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM