Two y axes for the same data - but different scale

六月ゝ 毕业季﹏ 提交于 2019-12-24 19:14:46

问题


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")) 

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.

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

回答1:


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(.))



回答2:


If you want to add second y-axis in same scale with the prior one, it can be implemented like this: scale_y_log10(sec_axis = sec_axis(~.*, name="dB")



来源:https://stackoverflow.com/questions/51048987/two-y-axes-for-the-same-data-but-different-scale

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!