使用 ggplot 绘制具有两个 y 刻度的图形
Plot with ggplot a graph with two y scales
所以我想在一张图中编译多个数据框。对于我的 x 值,我有天数,对于 y,我有某事发生的频率。
我的问题是其中一个数据帧 (df3) 的范围从 0 到 3,其他的从 0 到 50。
虚拟数据帧:
day<-c(1,2,3)
b<-c(23,44,22)
c<-c(12,35,49)
d<-c(1,1,3)
df1<-data.frame(day,b)
df2<-data.frame(day,c)
df3<-data.frame(day,d)
ggplot()+
geom_line(data=df1, aes(x=day, y=df1$`b`), color="red") +
geom_line(data=df2, aes(x=day, y=df2$`c` ), color="green")+
geom_line(data=df3, aes(x=day, y=df3$`d` ), color="blue")+
labs(x="Days", y="Number of occurrences")
这很好用,但我想为 df1 和 df2 创建一个不同的比例,另一个为 df3。
更新:
我正在尝试这个,但它会覆盖之前的比例:
d<-ggplot()+
geom_line(data=df1, aes(x=day, y=df1$`a`), color="red") +
geom_line(data=df2, aes(x=day, y=df2$`b` ), color="green")+
scale_y_continuous(limits=c(0, 50))+
labs(x="Days", y="Number of occurrences")
d+geom_line(data=df3, aes(x=day, y=df3$`c` ), color="blue")+
scale_y_continuous(limits=c(0, 3))
我想你可以使用 ggplot2
的 sec.axis
参数来做到这一点:
d<-ggplot()+
geom_line(data=df1, aes(x=day, y=df1$`1`), color="red") +
geom_line(data=df2, aes(x=day, y=df2$`1` ), color="green")+
scale_y_continuous(limits=c(0, 50))+
labs(x="Days", y="Number of occurrences")
d+geom_line(data=df3, aes(x=day, y=df3$`1` ), color="blue")+
scale_y_continuous(limits=c(0, 3),
sec.axis = sec_axis(~ . *scale_of_the_new_axis, name = "name of the new axis")
)
请注意,我在您的代码中添加了这一行:
sec.axis = sec_axis(~ . *scale_of_the_new_axis, name = "name of the new axis")
编辑:
我对 df3 的数据应用了一个转换,然后我应用了转换的逆运算以获得 df3 的实际值反映在新轴上。
ggplot()+
geom_line(data=df1, aes(x=day, y=b), color="red") +
geom_line(data=df2, aes(x=day, y=c ), color="green")+
geom_line(data=df3, aes(x=day, y=d*50/3), color="blue")+
scale_y_continuous(limits=c(0, 50),
sec.axis = sec_axis(~ . *3/50, name = "name of the new axis"))+
labs(x="Days", y="Number of occurrences")
结果是这样的:
如果这是你想要的,请告诉我。
所以我想在一张图中编译多个数据框。对于我的 x 值,我有天数,对于 y,我有某事发生的频率。 我的问题是其中一个数据帧 (df3) 的范围从 0 到 3,其他的从 0 到 50。
虚拟数据帧:
day<-c(1,2,3)
b<-c(23,44,22)
c<-c(12,35,49)
d<-c(1,1,3)
df1<-data.frame(day,b)
df2<-data.frame(day,c)
df3<-data.frame(day,d)
ggplot()+
geom_line(data=df1, aes(x=day, y=df1$`b`), color="red") +
geom_line(data=df2, aes(x=day, y=df2$`c` ), color="green")+
geom_line(data=df3, aes(x=day, y=df3$`d` ), color="blue")+
labs(x="Days", y="Number of occurrences")
这很好用,但我想为 df1 和 df2 创建一个不同的比例,另一个为 df3。
更新:
我正在尝试这个,但它会覆盖之前的比例:
d<-ggplot()+
geom_line(data=df1, aes(x=day, y=df1$`a`), color="red") +
geom_line(data=df2, aes(x=day, y=df2$`b` ), color="green")+
scale_y_continuous(limits=c(0, 50))+
labs(x="Days", y="Number of occurrences")
d+geom_line(data=df3, aes(x=day, y=df3$`c` ), color="blue")+
scale_y_continuous(limits=c(0, 3))
我想你可以使用 ggplot2
的 sec.axis
参数来做到这一点:
d<-ggplot()+
geom_line(data=df1, aes(x=day, y=df1$`1`), color="red") +
geom_line(data=df2, aes(x=day, y=df2$`1` ), color="green")+
scale_y_continuous(limits=c(0, 50))+
labs(x="Days", y="Number of occurrences")
d+geom_line(data=df3, aes(x=day, y=df3$`1` ), color="blue")+
scale_y_continuous(limits=c(0, 3),
sec.axis = sec_axis(~ . *scale_of_the_new_axis, name = "name of the new axis")
)
请注意,我在您的代码中添加了这一行:
sec.axis = sec_axis(~ . *scale_of_the_new_axis, name = "name of the new axis")
编辑:
我对 df3 的数据应用了一个转换,然后我应用了转换的逆运算以获得 df3 的实际值反映在新轴上。
ggplot()+
geom_line(data=df1, aes(x=day, y=b), color="red") +
geom_line(data=df2, aes(x=day, y=c ), color="green")+
geom_line(data=df3, aes(x=day, y=d*50/3), color="blue")+
scale_y_continuous(limits=c(0, 50),
sec.axis = sec_axis(~ . *3/50, name = "name of the new axis"))+
labs(x="Days", y="Number of occurrences")
结果是这样的:
如果这是你想要的,请告诉我。