[英]Equal size for multiple panels with different y-axis scales in lattice
I have multiple variables of a time series that differ in their scales. 我有一个时间序列的多个变量,它们的比例不同。 I want to plot each variable over time in a single-page, and each plot will have its own y-axis.
我想在一个页面中绘制随时间变化的每个变量,并且每个图都有其自己的y轴。 Seems to be easy, but I have a symmetry problem, since the plots that have higher values for y-axis were flattened to the right compared with the ones with smaller values for y-axis.
似乎很容易,但是我有一个对称性问题,因为与y轴较小的图相比,y轴具有较高的值的图向右展平。 Another problem with the panel size appeared when I decided to keep the x-axis only in two plots.
当我决定只在两个图中保留x轴时,面板尺寸出现了另一个问题。 These panels became more flattened than the others.
这些面板比其他面板更加扁平。
I'm relatively new to lattice and I have searched a lot with no success. 我对格网还比较陌生,并且搜索了很多但都没有成功。 First I tried to arrange the plots with
grid.arrange
, but I can't modify a specific panel with this function. 首先,我尝试使用
grid.arrange
布置图,但是我不能使用此功能修改特定的面板。 So I tried to arrange plots with print
and then use panel.widths
and panel.heights
. 所以我试着用
print
布置图,然后使用panel.widths
和panel.heights
。 but it doesn't give the exactly equal size for all panels. 但是它并不能为所有面板提供完全相同的尺寸。 Any suggestions to get multiple panels with equal sizes considering different y-axis and x-axis presence/absence?
考虑到不同的y轴和x轴是否存在,是否有建议获得多个具有相同大小的面板? Example below:
下面的例子:
#Data
a<-c(1058.2557,821.2002,1004.5201,296.8243,374.3730,746.0718,954.6511,264.7352)
b<-c(100,60,40,36,42,32,42,32)
c<-c(116.610418,164.462337,47.862511,12.613479,4.253702,39.868584,21.591731,6.037917)
d<-c(4,10,3,2,1,5,11,13)
e<-c(20,30,10,50,21,60,20,70)
est1<-c("16:00","19:00","22:00","01:00","04:00","07:00","10:00","13:00")
newest1<-factor(est1,levels=unique(est1))
mysettings<-list(layout.heights=list(top.padding=-1,bottom.padding=-1),
layout.widths=list(right.padding=-2))
plo1<-barchart(a~newest1,scales=list(x=list(alternating=0)),par.settings=mysettings)
plo2<-barchart(b~newest1,scales=list(x=list(alternating=0)),par.settings=mysettings)
plo3<-barchart(c~newest1,scales=list(x=list(alternating=0)),par.settings=mysettings)
plo4<-barchart(d~newest1,scales=list(x=list(rot=45)),par.settings=mysettings)
plo5<-barchart(e~newest1,scales=list(x=list(rot=45)),par.settings=mysettings)
trellis.device(windows, height=6, width=7)
print(plo1, split=c(1,1,2,3),more=T)
print(plo2, split=c(2,1,2,3),more=T)
print(plo3, split=c(1,2,2,3),more=T)
print(plo4, split=c(2,2,2,3),more=T)
print(plo5, split=c(1,3,2,3),more=F)
Generally you wouldn't layout related plots like that in lattice. 通常,您不会在晶格中布置类似的图。 You would typically use a grouping variable.
通常,您将使用分组变量。 For this to work, you need all your data in one data.frame
为此,您需要在一个data.frame中存储所有数据。
dd <- data.frame(make.groups(a=a,b=b,c=c,d=d,e=e), newest1=newest1)
And to make things look a bit nicer i'll define a custom axis function 为了使外观更好看,我将定义一个自定义轴函数
axis.yout<- function(side, ...) {
if(side %in% c("left", "right")) {
if (panel.number() %% 2 == which(c("right","left")==side)-1) {
panel.axis(side = side, outside =TRUE)
}
} else {
axis.default(side = side, ...)
}
}
now I plot with 现在我用
barchart(data~newest1 | which, dd, layout=c(2,3),
scales=list(alternating=T, y=list(relation="free")),
par.settings=list(layout.widths=list(right.padding=5, axis.panel = c(1, 0))),
axis=axis.yout
)
which result in 导致
which all share a common x-axis while allowing for free and independently labeled y-axis. 它们共享一个共同的x轴,同时允许自由且独立标记的y轴。 And the spacing/passing is all consistent because we used a single call to lattice.
而且间距/传递都是一致的,因为我们使用了一次晶格调用。 Normally you wouldn't bother with a custom axis function like this, but when the scales relation is "free", lattice gets a bit grumpy about alternating labels.
通常,您不会为这样的自定义轴功能而烦恼,但是,当比例关系为“自由”时,晶格对于交替标签会有些脾气。
I am sure someone will post a nice lattice
solution. 我相信有人会发布一个不错的
lattice
解决方案。 Meanwhile, you may consider a ggplot
alternative. 同时,您可以考虑使用
ggplot
替代方法。
library(reshape2)
library(ggplot2)
First, collect your vectors in a data frame, and reshape data from a wide to a long format: 首先,将您的向量收集到一个数据框中,然后将数据从宽格式整形为长格式:
df <- data.frame(newest1, a, b, c, d, e)
df2 <- melt(df, id.var = "newest1")
Plot the data in separate facets
, one facet for each of the original vectors (which in the melt
ed data ("df2") appear as different levels of the "variable" variable). 将数据绘制在单独的
facets
面中,每个原始向量的一个构面(在melt
数据(“ df2”)中显示为“变量”变量的不同级别)。 We allow independent (" free
") y axis scales
in each facet: 我们在每个方面都允许独立的(“
free
”)y轴scales
:
ggplot(data = df2, aes(x = newest1, y = value)) +
geom_bar(stat = "identity") +
facet_wrap(~ variable, scales = "free_y") +
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.