ggplot2:在两条线的交点处添加点

时间:2018-11-18 01:00:19

标签: r ggplot2

我很难在均值a和密度b与均值b和密度a的交点处添加点。

我尝试使用geom_point(data.frame(x = c(2,3),y = rep(dnorm(2,mean = 3),2),fill =“ green”,...),但我坚持出现错误,不知道该怎么办。

ggplot(data = data.frame(x = c(-1, 6)), aes(x)) +
stat_function(fun = dnorm, n = 100, args = list(mean = 2, sd = 1), aes(linetype = "a")) +
stat_function(fun = dnorm, n = 100, args = list(mean = 3, sd = 1), aes(linetype = "b")) +
scale_linetype_manual("Density Function", values = c(1, 2)) +
labs(x = "Value", y = "Probability Density") +
geom_vline(aes(xintercept = 2, colour = "mean1"), show.legend = NA) +
geom_vline(aes(xintercept = 3, colour = "mean2"), show.legend = NA, linetype = 2) +
scale_colour_manual("Mean", values = c(mean1 = "#F8766D", mean2 = "#C77CFF"), 
                  labels = c("Mean a", "Mean b")) +
ggtitle("Add points at intersection")

Plot

不幸的是,@内特的建议效果不佳:

ggplot() +
  stat_function(fun = dnorm, n = 100, args = list(mean = 2, sd = 1), aes(linetype = "a")) +
  stat_function(fun = dnorm, n = 100, args = list(mean = 3, sd = 1), aes(linetype = "b")) +
  geom_point(x = c(2,3), y = rep(dnorm(2, mean = 3), 2), color = "green") +
  scale_linetype_manual("Density Function", values = c(1, 2)) +
  labs(x = "Value", y = "Probability Density") +
  geom_vline(aes(xintercept = 2, colour = "mean1"), show.legend = NA) +
  geom_vline(aes(xintercept = 3, colour = "mean2"), show.legend = NA, linetype = 2) +
  scale_colour_manual("Mean", values = c(mean1 = "#F8766D", mean2 = "#C77CFF"), 
                      labels = c("Mean a", "Mean b")) +
  ggtitle("Add points at intersection")

plot2

1 个答案:

答案 0 :(得分:1)

对我来说很好:

library(ggplot2)

ggplot(data = data.frame(x = c(-1, 6)), aes(x)) +
    stat_function(fun = dnorm, n = 100, args = list(mean = 2, sd = 1), aes(linetype = "a")) +
    stat_function(fun = dnorm, n = 100, args = list(mean = 3, sd = 1), aes(linetype = "b")) +
    scale_linetype_manual("Density Function", values = c(1, 2)) +
    labs(x = "Value", y = "Probability Density") +
    geom_vline(aes(xintercept = 2, colour = "mean1"), show.legend = NA) +
    geom_vline(aes(xintercept = 3, colour = "mean2"), show.legend = NA, linetype = 2) +
    scale_colour_manual("Mean", values = c(mean1 = "#F8766D", mean2 = "#C77CFF"), 
                        labels = c("Mean a", "Mean b")) +
    ggtitle("Add points at intersection") +
    geom_point(x = c(2,3), y = rep(dnorm(2, mean = 3), 2), color = "green")

enter image description here

也许软件包版本问题? (我对此表示怀疑...)

devtools::session_info()
Session info ------------------------------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.5.0 (2018-04-23)
 system   x86_64, darwin15.6.0        
 ui       RStudio (1.1.456)           
 language (EN)                        
 collate  en_US.UTF-8                 
 tz       America/New_York            
 date     2018-11-17                  

Packages ----------------------------------------------------------------------------------------------------------------------
 package    * version    date       source                            
 assertthat   0.2.0      2017-04-11 CRAN (R 3.5.0)                    
 base       * 3.5.0      2018-04-24 local                             
 bindr        0.1.1      2018-03-13 CRAN (R 3.5.0)                    
 bindrcpp     0.2.2      2018-03-29 CRAN (R 3.5.0)                    
 colorspace   1.3-2      2016-12-14 CRAN (R 3.5.0)                    
 compiler     3.5.0      2018-04-24 local                             
 crayon       1.3.4      2018-11-02 Github (r-lib/crayon@0398b12)     
 datasets   * 3.5.0      2018-04-24 local                             
 devtools     1.13.6     2018-06-27 CRAN (R 3.5.0)                    
 digest       0.6.18     2018-10-10 cran (@0.6.18)                    
 dplyr        0.7.8      2018-11-10 CRAN (R 3.5.0)                    
 ggplot2    * 3.1.0.9000 2018-11-05 Github (tidyverse/ggplot2@15ddc22)
 glue         1.3.0      2018-07-17 cran (@1.3.0)                     
 graphics   * 3.5.0      2018-04-24 local                             
 grDevices  * 3.5.0      2018-04-24 local                             
 grid         3.5.0      2018-04-24 local                             
 gtable       0.2.0      2016-02-26 CRAN (R 3.5.0)                    
 labeling     0.3        2014-08-23 CRAN (R 3.5.0)                    
 lazyeval     0.2.1      2017-10-29 CRAN (R 3.5.0)                    
 magrittr     1.5        2014-11-22 CRAN (R 3.5.0)                    
 memoise      1.1.0      2017-04-21 CRAN (R 3.5.0)                    
 methods    * 3.5.0      2018-04-24 local                             
 munsell      0.5.0      2018-06-12 cran (@0.5.0)                     
 pillar       1.3.0.9000 2018-08-18 Github (r-lib/pillar@3fabb4e)     
 pkgconfig    2.0.2      2018-08-16 cran (@2.0.2)                     
 plyr         1.8.4      2016-06-08 CRAN (R 3.5.0)                    
 purrr        0.2.5      2018-05-29 cran (@0.2.5)                     
 R6           2.3.0      2018-10-04 cran (@2.3.0)                     
 Rcpp         1.0.0      2018-11-07 cran (@1.0.0)                     
 rlang        0.3.0.1    2018-10-25 cran (@0.3.0.1)                   
 rstudioapi   0.8        2018-10-02 cran (@0.8)                       
 scales       1.0.0      2018-08-09 cran (@1.0.0)                     
 stats      * 3.5.0      2018-04-24 local                             
 tibble       1.4.2      2018-01-22 CRAN (R 3.5.0)                    
 tidyselect   0.2.5      2018-10-11 cran (@0.2.5)                     
 tools        3.5.0      2018-04-24 local                             
 utils      * 3.5.0      2018-04-24 local                             
 withr        2.1.2.9000 2018-10-18 Github (jimhester/withr@be57595)  
 yaml         2.1.19     2018-05-01 cran (@2.1.19)