简体   繁体   中英

Two y axes on the same scale on the same plot in R

I am currently trying to reproduce a plot that looks like this: VIP价与绝对回归系数的加权和与质量的比

Ignoring the graduated scales on the right side, there are two y-axes on the graph. X is the VIP score, and the y scale is determined by the weighted sum of absolute regression coefficients, however this scale is NOT visible, and masses are seen on the left y axis. The masses are categorical variables, in this case, that match each value in the continuous variable of weighted sum of absolute regression coefficients.

My question is how do I use ggplot2, or another R package, to reproduce this? Labelling the points directly using ggrepel is not an option as there are too many masses in my dataset. Is there a way to create a scatterplot with two y axes BUT the second y axis is a categorical variable?

Sample data:

        Masses      Overall        VIP1      
1     82.07010  38.26669006 1.484957089
2     84.08570  34.22745192 1.328724766 
3     95.08570  38.65684978 1.500047945
4     96.08571  13.13685100 0.512968559
5     98.10140  36.07639404 1.400239372
6     99.04410  17.37079280 0.676731759
7    105.07530   9.38047849 0.367677099 
8    110.10130  36.66816959 1.423128458
9    111.10160  13.64197654 0.532506138
10   113.06040  10.09391101 0.395271714

This seems terrible, but it's what you are asking for. Calling your data dd :

ggplot(dd, aes(x = VIP1, y = Overall)) +
    geom_point() +
    scale_y_continuous(breaks = dd$Overall, labels = dd$Masses)

在此处输入图片说明

We use scale_y_continuous because the variable you want to define the y axis positions, Overall , is continuous.


Using this data:

dd = read.table(text = "        Masses      Overall        VIP1      
1     82.07010  38.26669006 1.484957089
2     84.08570  34.22745192 1.328724766 
3     95.08570  38.65684978 1.500047945
4     96.08571  13.13685100 0.512968559
5     98.10140  36.07639404 1.400239372
6     99.04410  17.37079280 0.676731759
7    105.07530   9.38047849 0.367677099 
8    110.10130  36.66816959 1.423128458
9    111.10160  13.64197654 0.532506138
10   113.06040  10.09391101 0.395271714", header = TRUE)

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