使用sec.axis时控制y轴的最简单方法是什么 [英] What is the simplest way to control the y-axis when using sec.axis

查看:74
本文介绍了使用sec.axis时控制y轴的最简单方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了有关缩放y轴的所有文章,在ggplot中使用sec.axis时,所有这些步骤中都有一些额外的步骤来控制y轴限制.

I have reading all post about scaling the y-axis and in all of them there are some extra steps in order to control the y-axis limits when using sec.axis in ggplot.

我有以下df.

structure(list(day = c(1, 3, 5, 7, 9), mean = c(0.000452620431539136, 
0.000244953967091816, 0.000409529176828165, 0.000621566432113383, 
0.000975471413145951), sd = c(0.000145928952108396, 7.48403498938327e-05, 
8.70694523628839e-05, 0.000265199022927143, 0.00076194983870935
), group = c("pi", "pi", "pi", "pi", "pi")), row.names = c(NA, 
-5L), class = c("tbl_df", "tbl", "data.frame"))

structure(list(day = c(1, 3, 5, 7), mean = c(NaN, 5.85880255563636, 
4.16535426125, 3.22060147866667), sd = c(NaN, 0.363838291664683, 
0.980379999667707, 1.17101416465057), group = c("Equi", "Equi", 
"Equi", "Equi")), row.names = c(NA, -4L), class = c("tbl_df", 
"tbl", "data.frame"))

我运行以下代码:

-ggplot(data=DI.pi.sum, aes(x=day, y=mean)) + geom_bar(stat = "identity", fill = "grey", size = 1.5) +
       geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), size = 0.1, width=.3,
                     position=position_dodge(1)) +
       geom_line(data=DI.Equi.sum, aes(x=day, y=mean/10000))  +
       geom_ribbon(data = DI.Equi.sum, 
                   aes(x=day, y = mean/10000, ymin=mean/10000-sd/10000, ymax=mean/10000+sd/10000),
                   alpha=0.2, fill = "grey40") +
       theme(panel.grid.major =  element_blank(),
             panel.grid.minor = element_blank(),
             panel.background = element_blank(),
             axis.line = element_line(colour = "black"),
             axis.text.x = element_text(face = "bold", size = 7),
             axis.title.y = element_text(face = "bold", size = 10),
             legend.direction = "vertical", legend.box = "horizontal") +
       scale_size(range = c(5, 15)) +
       scale_x_continuous(breaks = c(1, 3, 5, 7, 9), limits = c(0,10))  +
       scale_y_continuous(limits=c(0, 0.0020), sec.axis = sec_axis(~ . * 10000), name = "pi")  

产生以下图:

我不需要sec轴上升到40.我很高兴将其增加到20.

I do not need the sec axis to go up to 40. I would be great to get it to just 20.

建议?

谢谢.

推荐答案

要与辅助轴一起绘制的均值范围仅为3-6.我已经使用比例因子变量调整了对ggplot的调用,因此您可以尝试使用辅助轴的外观.缩放比例为10,000,则辅助轴范围为0-20.

The range of the mean which you want to plot with the secondary axis is only 3-6. I've adjusted your call to ggplot with a scaling factor variable so you can play around with how you want the secondary axis to look. A scaling factor of 10,000 gets you a secondary axis range from 0-20.


library(ggplot2)

scaling_factor = 10000

ggplot(data=DI.pi.sum, aes(x=day, y=mean)) + geom_bar(stat = "identity", fill = "grey", size = 1.5) +
  geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), size = 0.1, width=.3,
                position=position_dodge(1)) +
  geom_line(data=DI.Equi.sum, aes(x=day, y=mean/scaling_factor))  +
  geom_ribbon(data = DI.Equi.sum, 
              aes(x=day, y = mean/scaling_factor, ymin=mean/scaling_factor-sd/scaling_factor, ymax=mean/scaling_factor+sd/scaling_factor),
              alpha=0.2, fill = "grey40") +
  theme(panel.grid.major =  element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line(colour = "black"),
        axis.text.x = element_text(face = "bold", size = 7),
        axis.title.y = element_text(face = "bold", size = 10),
        legend.direction = "vertical", legend.box = "horizontal") +
  scale_size(range = c(5, 15)) +
  scale_x_continuous(breaks = c(1, 3, 5, 7, 9), limits = c(0,10))  +
  scale_y_continuous(limits=c(0, 0.0020), sec.axis = sec_axis(~ . * scaling_factor, name =  "secondary axis"), name = "pi")  


通常认为不适合使用双刻度y轴,尤其是使用条形,因此不建议使用.(摘自下面的几篇文章)

Using dual scaled y-axis especially with bars is generally considered inappropriate and should be discouraged. (Paraphrased from the Few article noted below)

有关此问题的讨论,请参见以下内容:我如何才能使用2个不同的y轴进行绘制?

See this for discussion on the issue: How can I plot with 2 different y-axes?, ggplot with 2 y axes on each side and different scales and the linked article by Stephen Few: http://www.perceptualedge.com/articles/visual_business_intelligence/dual-scaled_axes.pdf

reprex软件包(v0.3.0)创建于2020-05-25 sup>

Created on 2020-05-25 by the reprex package (v0.3.0)

这篇关于使用sec.axis时控制y轴的最简单方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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