简体   繁体   English

R中同一图上相同比例的两个y轴

[英]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. 忽略右侧的刻度尺,图形上有两个y轴。 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. X是VIP得分,y比例由绝对回归系数的加权和确定,但是该比例不可见,在左侧y轴上可以看到质量。 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? 我的问题是如何使用ggplot2或另一个R包来重现此内容? Labelling the points directly using ggrepel is not an option as there are too many masses in my dataset. 不能直接使用ggrepel标记点,因为数据集中的质量太多。 Is there a way to create a scatterplot with two y axes BUT the second y axis is a categorical variable? 有没有一种方法可以创建带有两个y轴的散点图,但第二个y轴是分类变量?

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 : 调用您的数据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. 我们使用scale_y_continuous因为变量要定义y轴位置, Overall ,是连续的。


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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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