两个y轴用于相同的数据-但比例不同 [英] Two y axes for the same data - but different scale

查看:190
本文介绍了两个y轴用于相同的数据-但比例不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个图形,以线性单位表示数据,但以对数y比例表示(下面的代码).

I've created a graph presenting data in linear units but in logarithmic y-scale (code below).

ggplot() + 
  geom_point(data=VH_lin, aes(x=VH_lin[,1], y=VH_lin[,2], colour = "green"), size=0.6) +
  scale_y_log10(breaks = c(0,0.01, 0.10, 1.00, 10), limit=c(-0.01,10)) + scale_x_continuous(breaks=c(0, 2000, 4000, 6000, 8000, 10000, 12000)) + 
  xlab("x") + ylab("y") +
  theme(panel.background = element_rect(fill = "white", colour = "grey50")) 

我想以分贝为单位添加第二个y刻度-这是对数单位.为了转换数据,我必须计算10 * log10(x),因此图上的数据分布应该相同-因为dB是对数的. 基本上,我想使用两个单位在同一地块上显示数据数据: 线性(但以对数标度表示-已在代码中显示)和dB.是否有可能?下面的图片(很糟糕)提出了我的想法.

I would like to add second y scale in decibels - which are logarithmic units. To transform data I have to calculate 10*log10(x) so the distribution of data on the plot should be the same - as dB are logarithmic. Basically, I would like to present the dame data on the same plot using two units: linear (but presented in logarithmic scale - already in the code) and dB. Is it possible? The pic below (poorly) presents my idea.

VH_lin数据示例:

Example of VH_lin data:

1 0 0.012729834
2 3.133577295 0.012729834
10 14.75257582 0.013739633
36 59.10725461 0.014644137
41 69.42152155 0.0103109
466 1180.242805 0.011991354
486 1204.63381 0.008985861
520 1256.814223 0.008706877

推荐答案

您可以添加第二个轴,同时将数据点保持在原始比例中,如下所示:

You can add the second axis while keeping data points in an original scale as follows:

ggData <- data.frame(x=rnorm(50), y=rnorm(50, mean=1000, sd=50) )
summary(ggData)

ggplot(ggData, aes(x=x, y=y) ) + 
 geom_point() + 
 scale_y_continuous(sec.axis = ~ 10*log10(.))

这篇关于两个y轴用于相同的数据-但比例不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆