缩放两个单独的y轴标签ggplot2

时间:2019-12-22 14:04:28

标签: r ggplot2

我正在绘制两个单独的y轴标签,以显示耗电量和温度之间的关系。我似乎对第一个y轴失去控制。我希望此比例约为0.2-0.4。我的体温表很好。我希望我的消费规模看起来像这样。

enter image description here

但是在用相同的代码添加第二个音阶之后,它会更改为此。

enter image description here

这是我的代码:

ggplot()+   
    geom_line(data=average_total, aes(x=day,y=average_day,col="Consumption"))+
    scale_y_continuous(sec.axis = sec_axis(~.*50, name = "Temperature [C]"))+
    geom_line(data=temp, aes(x=day, y=day_temp/50, col="Temperature"))+
    scale_color_manual("",values=c("skyblue4","green4"),
                    labels=c("Consumption","Temperature"))+
labs(title = "Yearly average consumption\nand ambient temperature",
       x = "Year day",
       y = "Electricity consumption Kwh")+
theme_linedraw()

我的数据:

day average_day
<dbl>   <dbl>
1   0.3226814
2   0.3248489
3   0.3254643
4   0.3286167
5   0.3281448
6   0.3346636

day day_temp
<dbl>   <dbl>
1   7.49
2   10.82
3   11.41
4   10.79
5   10.66
6   8.61

关于如何在不影响温度轴的情况下调整第一个y轴刻度的任何想法?

2 个答案:

答案 0 :(得分:2)

我对您的代码进行了一些更改。除了使用您最初的线性变换~.*50之外,我还使用~.*200-40和相应的反向变换作为y=(day_temp+40)/200。我还更改了示例数据以显示两个变量的范围。您可能需要调整这些值以适合实际数据。

library(ggplot2)

average_total <- data.frame(day = c(1,2,3,4,5,6),
                            average_day = c(0.38, 0.25, 0.21, 0.22, 0.38, 0.40))
temp <- data.frame(day = c(1,2,3,4,5,6),
                   day_temp = c(0.5,20,30,38,25,4))

ggplot()+   
  geom_line(data=average_total, aes(x=day,y=average_day,col="Consumption"))+
  scale_y_continuous(sec.axis = sec_axis(~.*200-40, name = "Temperature [C]"))+
  geom_line(data=temp, aes(x=day, y=(day_temp+40)/200, col="Temperature"))+
  scale_color_manual("",values=c("skyblue4","green4"),
                     labels=c("Consumption","Temperature"))+
  labs(title = "Yearly average consumption\nand ambient temperature",
       x = "Year day",
       y = "Electricity consumption Kwh")+
  theme_linedraw()

reprex package(v0.3.0)于2019-12-23创建

答案 1 :(得分:0)

首先,提供的数据不能完全代表您绘制的数据,因此绘制的图将有所不同。

我相信您在问如何控制主轴(电耗)上的y_axis断点。您可以通过将breaks = seq(.25, .35, by = .025)添加到scale_y_continuous函数调用中来实现。


library(ggplot2)

average_total <- data.frame(day = c(1,2,3,4,5,6),
                            average_day = c(0.3226814, 0.3248489, 0.3254643, 0.3286167, 0.3281448, 0.3346636))
temp <- data.frame(day = c(1,2,3,4,5,6),
                   day_temp = c(7.49,10.82,11.41,10.79,10.66,8.61))

# Initial PLot 

ggplot()+   
  geom_line(data=average_total, aes(x=day,y=average_day,col="Consumption"))+
  scale_y_continuous(sec.axis = sec_axis(~.*50, name = "Temperature [C]"))+
  geom_line(data=temp, aes(x=day, y=day_temp/50, col="Temperature"))+
  scale_color_manual("",values=c("skyblue4","green4"),
                     labels=c("Consumption","Temperature"))+
  labs(title = "Yearly average consumption\nand ambient temperature",
       x = "Year day",
       y = "Electricity consumption Kwh")+
  theme_linedraw()


# Adjusted Plot

ggplot()+   
  geom_line(data=average_total, aes(x=day,y=average_day,col="Consumption"))+
  scale_y_continuous(breaks = seq(.25, .35, by = .025),
                     sec.axis = sec_axis(~.*50, name = "Temperature [C]"))+
  geom_line(data=temp, aes(x=day, y=day_temp/50, col="Temperature"))+
  scale_color_manual("",values=c("skyblue4","green4"),
                     labels=c("Consumption","Temperature"))+
  labs(title = "Yearly average consumption\nand ambient temperature",
       x = "Year day",
       y = "Electricity consumption Kwh")+
  theme_linedraw()

reprex package(v0.3.0)于2019-12-22创建

因为您正在使用两个单独的y_axis,所以在不同的比例尺上,您要么要a。)主轴被截断(如上所示),要么b。)您将拥有一个完整的轴(可以可以通过设置限制来执行此操作),但低级别的断点将导致每.025单位产生一个轴值。这将是非常压倒性的。