需要帮助使用ggplot向我的图形添加第二个y轴

时间:2019-06-05 18:17:13

标签: r ggplot2

我需要在图形的右侧添加一个Y轴,以显示发生频率,但是我不确定如何使频率显示在右侧。

我当时想我可以创建一个具有频率的向量,然后将其传递给scale_y_continuous(sec.axis = sec_axis())。我很难弄清楚该怎么做,我知道需要一个公式,但是我希望频率与每个条形图和相应的名称对齐。

p<- ggplot(B, aes(Analytes, Concentration, ylab="Analyte"), na.rm=TRUE)

p + geom_boxplot(fill= "gray", na.rm=TRUE) + coord_flip() +
  geom_point(aes(color = Analyte)) + 
scale_y_continuous(sec.axis = sec_axis(?, name = "Frequency"))


B

             Analyte Concentration   MRL units Quarter Year             Source  X X.1   X.2
1                               NA    NA                 NA                    NA  NA    NA
2       Acesulfame-K     0.1200000 1e-02  µg/L    1st  2018                raw NA  NA    NA
3       Acesulfame-K     0.0800000 1e-02  µg/L    1st  2018     finished - WTF NA  NA    NA
4       Acesulfame-K     0.0700000 1e-02  µg/L    1st  2018 finished - rechlor NA  NA    NA
5       Acesulfame-K     0.1200000 1e-02  µg/L    2nd  2018                raw NA  NA    NA
6       Acesulfame-K     0.1200000 1e-02  µg/L    2nd  2018     finished - WTF NA  NA    NA
7       Acesulfame-K     0.0600000 1e-02  µg/L    2nd  2018 finished - rechlor NA  NA    NA
8       Acesulfame-K     0.0600000 1e-02  µg/L    3rd  2018                raw NA  NA    NA
9       Acesulfame-K     0.0600000 1e-02  µg/L    3rd  2018     finished - WTF NA  NA    NA
10      Acesulfame-K     0.1600000 1e-02  µg/L    3rd  2018 finished - rechlor NA  NA    NA
11      Acesulfame-K     0.0600000 1e-02  µg/L    4th  2018                raw NA  NA    NA
12      Acesulfame-K     0.0600000 1e-02  µg/L    4th  2018     finished - WTF NA  NA    NA
13      Acesulfame-K     0.0200000 1e-02  µg/L    4th  2018 finished - rechlor NA  NA    NA
14      Acesulfame-K     0.1200000 1e-02  µg/L    1st  2017                raw NA  NA    NA
15      Acesulfame-K     0.1100000 1e-02  µg/L    1st  2017     finished - WTF NA  NA    NA
16      Acesulfame-K     0.1500000 1e-02  µg/L    2nd  2017                raw NA  NA    NA
17      Acesulfame-K     0.1700000 1e-02  µg/L    2nd  2017     finished - WTF NA  NA    NA
18      Acesulfame-K     0.6900000 1e-02  µg/L    2nd  2017 finished - rechlor NA  NA    NA
19      Acesulfame-K     0.1100000 1e-02  µg/L    3rd  2017                raw NA  NA    NA
20      Acesulfame-K     0.1100000 1e-02  µg/L    3rd  2017     finished - WTF NA  NA    NA
21      Acesulfame-K     0.0400000 1e-02  µg/L    3rd  2017 finished - rechlor NA  NA    NA
22      Acesulfame-K     0.1200000 1e-02  µg/L    4th  2017                raw NA  NA    NA
23      Acesulfame-K     0.1000000 1e-02  µg/L    4th  2017     finished - WTF NA  NA    NA
24      Acesulfame-K     0.0200000 1e-02  µg/L    4th  2017 finished - rechlor NA  NA    NA
25      Acesulfame-K     0.1300000 1e-02  µg/L    3rd  2016     finished - WTF NA  NA    NA
26      Acesulfame-K     0.0900000 1e-02  µg/L    3rd  2016 finished - rechlor NA  NA    NA
27      Acesulfame-K     0.1300000 1e-02  µg/L    4th  2016                raw NA  NA    NA
28      Acesulfame-K     0.1000000 1e-02  µg/L    4th  2016     finished - WTF NA  NA    NA
29      Acesulfame-K     0.4100000 1e-02  µg/L    4th  2016 finished - rechlor NA  NA    NA
30      Acesulfame-K     0.1300000 1e-02 µg/L2    3rd  2016                raw NA  NA 1e-02
31          Atenolol     0.0020000 1e-03  µg/L    1st  2010                raw NA  NA    NA
32          Atenolol     0.0040000 1e-03  µg/L    1st  2010     finished - WTF NA  NA    NA
33          Atenolol     0.0040000 1e-03  µg/L    1st  2010 finished - rechlor NA  NA    NA
34          Atenolol     0.0050000 1e-03  µg/L    2nd  2010                raw NA  NA    NA
35          Atenolol     0.0050000 1e-03  µg/L    2nd  2010     finished - WTF NA  NA    NA
36          Atenolol     0.0050000 1e-03  µg/L    2nd  2010 finished - rechlor NA  NA    NA
37   Chloramphenicol     0.0130000 5e-03  µg/L    3rd  2014     finished - WTF NA  NA    NA
38  cis-Testosterone     0.5000000 1e-01  ng/L    1st  2018                raw NA  NA 1e-04
39  cis-Testosterone     0.3000000 1e-01  ng/L    1st  2018     finished - WTF NA  NA 1e-04
40  cis-Testosterone     1.4000000 1e-01  ng/L    1st  2016                raw NA  NA 1e-04
41  cis-Testosterone     0.7000000 1e-01  ng/L    1st  2016     finished - WTF NA  NA 1e-04
42  cis-Testosterone     0.4000000 1e-01  ng/L    2nd  2016                raw NA  NA 1e-04
43  cis-Testosterone     0.3000000 1e-01  ng/L    2nd  2016 finished - rechlor NA  NA 1e-04
44  cis-Testosterone     0.2000000 1e-01  ng/L    1st  2015                raw NA  NA 1e-04
45  cis-Testosterone     0.2000000 1e-01  ng/L    1st  2015     finished - WTF NA  NA 1e-04
46  cis-Testosterone     0.4000000 1e-01  ng/L    1st  2014                raw NA  NA 1e-04
47  cis-Testosterone     0.9000000 1e-01  ng/L    1st  2014     finished - WTF NA  NA 1e-04
48  cis-Testosterone     0.5000000 1e-01  ng/L    1st  2013                raw NA  NA 1e-04
49  cis-Testosterone     0.4000000 1e-01  ng/L    1st  2013     finished - WTF NA  NA 1e-04
50  cis-Testosterone     0.0075000 1e-04  µg/L    1st  2011                raw NA  NA    NA
51  cis-Testosterone     0.0055000 1e-04  µg/L    1st  2011     finished - WTF NA  NA    NA
52          Cotinine     0.0010000 1e-03  µg/L    3rd  2018 finished - rechlor NA  NA    NA
53          Cotinine     0.0010000 1e-03  µg/L    4th  2018                raw NA  NA    NA
54          Cotinine     0.0010000 1e-03  µg/L    4th  2018     finished - WTF NA  NA    NA
55          Cotinine    -0.9208188 1e-03  µg/L    1st  2017     finished - WTF NA  NA    NA
56          Cotinine    -0.9208188 1e-03  µg/L    2nd  2017 finished - rechlor NA  NA    NA
57          Cotinine    -1.0969100 1e-03  µg/L    4th  2015 finished - rechlor NA  NA    NA
58          Cotinine    -1.1549020 1e-03  µg/L    3rd  2014 finished - rechlor NA  NA    NA
59          Cotinine    -0.9208188 1e-03  µg/L    1st  2013                raw NA  NA    NA
60          Cotinine    -0.9208188 1e-03  µg/L    2nd  2013     finished - WTF NA  NA    NA
61          Cotinine    -1.2218487 1e-03  µg/L    4th  2013                raw NA  NA    NA
62          Cotinine    -1.2218487 1e-03  µg/L    4th  2013     finished - WTF NA  NA    NA
63          Cotinine    -1.2218487 1e-03  µg/L    1st  2012                raw NA  NA    NA
64          Cotinine    -0.7958800 1e-03  µg/L    1st  2012     finished - WTF NA  NA    NA
65          Cotinine    -1.2218487 1e-03  µg/L    1st  2012 finished - rechlor NA  NA    NA
66          Cotinine    -1.2218487 1e-03  µg/L    2nd  2012                raw NA  NA    NA
67          Cotinine    -1.6989700 1e-03  µg/L    2nd  2012     finished - WTF NA  NA    NA
68          Cotinine    -0.9208188 1e-03  µg/L    2nd  2012 finished - rechlor NA  NA    NA
69          Cotinine    -0.9586073 1e-03  µg/L    3rd  2012                raw NA  NA    NA
70          Cotinine    -0.8239087 1e-03  µg/L    3rd  2012     finished - WTF NA  NA    NA
71          Cotinine    -0.7695511 1e-03  µg/L    3rd  2012 finished - rechlor NA  NA    NA
72          Cotinine    -0.1611509 1e-03  µg/L    4th  2012                raw NA  NA    NA
73          Cotinine    -0.9586073 1e-03  µg/L    4th  2012     finished - WTF NA  NA    NA
74          Cotinine    -0.9586073 1e-03  µg/L    4th  2011                raw NA  NA    NA
75          Cotinine    -1.3979400 1e-03  µg/L    4th  2010                raw NA  NA    NA
76          Cotinine    -0.9208188 1e-03  µg/L    4th  2010     finished - WTF NA  NA    NA
77          Cotinine    -1.0000000 1e-03  µg/L    4th  2010 finished - rechlor NA  NA    NA
78          Cotinine    -1.6989700 1e-03  µg/L    3rd  2009                raw NA  NA    NA
79              DEET    -0.8860566 5e-03  µg/L    1st  2018                raw NA  NA    NA
80              DEET    -1.0457575 5e-03  µg/L    1st  2018     finished - WTF NA  NA    NA
81              DEET    -0.8860566 5e-03  µg/L    1st  2018 finished - rechlor NA  NA    NA
82              DEET    -1.0000000 5e-03  µg/L    2nd  2018                raw NA  NA    NA
83              DEET    -0.8860566 5e-03  µg/L    3rd  2018                raw NA  NA    NA
84              DEET    -2.6989700 5e-03  µg/L  
85              DEET    -2.3979400 5e-03  µg/L   
86              DEET    -2.3979400 5e-03  µg/L   
87              DEET    -2.3010300 5e-03  µg/L    
88              DEET    -2.3010300 5e-03  µg/L 
89              DEET    -2.3010300 5e-03  µg/L   
90              DEET    -0.3979400 5e-03  µg/L    
91              DEET    -2.1249387 5e-03  µg/L    
92              DEET    -2.2596373 5e-03  µg/L    
93              DEET    -3.0000000 5e-03  µg/L    
94              DEET    -3.0000000 5e-03  µg/L  
95              DEET    -3.0000000 5e-03  µg/L    
96              DEET    -3.0000000 5e-03  µg/L   
97              DEET    -3.0000000 5e-03  µg/L    
98              DEET    -3.0000000 5e-03 µg/L2   
99              DEET    -3.0000000 5e-03  µg/L    
100             DEET    -3.0000000 5e-03  µg/L   
 [ reached 'max' / getOption("max.print") -- omitted 275 rows ]

我希望输出在左x轴上显示名称,并在右y轴上显示相应的频率。

1 个答案:

答案 0 :(得分:0)

添加频率作为重点可能不是最好的主意。一个有用的方法是将计数添加为文本。在这里,我向您展示如何做。我没有使用您的数据,但我模拟了一个具有相同名称的示例df(请为以后的问题考虑)。

模拟数据

B <- data.frame(
  Analyte = sample(x = c("Acesulfame-k", "Atenolol","Cotinine"),
                   prob = c(0.5, 0.25, 0.25), size = 100,
                   replace = TRUE),
  Concentration = rnorm(100, mean = 1, sd = 0.7)
)

定义用于设置文本位置计数的功能

stat_box_data <- function(y, upper_limit = max(B$Concentration) * 1.15) {
    return( 
      data.frame(
        y = 0.95 * upper_limit,
        label = paste('count =', length(y), '\n')
      )
    )
  }

创建情节

ggplot(data = B, aes(x = Analyte, y = Concentration)) +
  geom_boxplot() +
  stat_summary(
    fun.data = stat_box_data, 
    geom = "text", 
    hjust = 0.5,
    vjust = 0.9
  ) +
  coord_flip() 

情节

enter image description here