几个 ggplots 的相同宽度与 grid.arrange 函数相结合
Same width of few ggplots combined with grid.arrange function
有没有办法设置 ggplot 的宽度?
我正在尝试将三个时间图合并到一栏中。
由于 y 轴值,图具有不同的宽度(两个图的轴值在范围 (-20 , 50) 内,一个 (18 000, 25 000) - 这使得图更薄)。
我想让所有地块的宽度完全相同。
plot1<-ggplot(DATA1, aes(x=Date,y=Load))+
geom_line()+
ylab("Load [MWh]") +
scale_x_date(labels = date_format("%m/%y"),breaks = date_breaks("months"))+
theme_minimal()+
theme(panel.background=element_rect(fill = "white") )
plot2<-ggplot(DATA1, aes(x=Date,y=Temperature))+
geom_line()+
ylab("Temperature [C]") +
scale_x_date(labels = date_format("%m/%y"),breaks = date_breaks("months"))+
theme_minimal()+
theme(panel.background=element_rect(fill = "white") )
plot3<-ggplot(DATA1, aes(x=Date,y=WindSpeed))+
geom_line()+
ylab("Wind Speed [km/h]") +
scale_x_date(labels = date_format("%m/%y"),breaks = date_breaks("months"))+
theme_minimal()+
theme(panel.background=element_rect(fill = "white") )
grid.arrange(plot1, plot2, plot3, nrow=3)
组合图如下所示:
您可以为此简单地使用分面。首先,您必须进行一些数据处理:
library(tidyr)
new_data = gather(DATA1, variable, value, Load, Temperature, WindSpeed)
这会将 Load
、Temperature
和 Windspeed
中的所有数据收集到一个大列中 (value
)。此外,还会创建一个额外的列 (variable
),用于指定向量中的哪个值属于哪个变量。
之后你可以绘制数据:
ggplot(new_data) + geom_line(aes(x = Date, y = value)) +
facet_wrap(~ variable, scales = 'free_y', ncol = 1)
现在 ggplot2 将处理所有繁重的工作。
ps 如果你提出问题 reproducible,我可以让我的答案可重现。
有没有办法设置 ggplot 的宽度?
我正在尝试将三个时间图合并到一栏中。 由于 y 轴值,图具有不同的宽度(两个图的轴值在范围 (-20 , 50) 内,一个 (18 000, 25 000) - 这使得图更薄)。 我想让所有地块的宽度完全相同。
plot1<-ggplot(DATA1, aes(x=Date,y=Load))+
geom_line()+
ylab("Load [MWh]") +
scale_x_date(labels = date_format("%m/%y"),breaks = date_breaks("months"))+
theme_minimal()+
theme(panel.background=element_rect(fill = "white") )
plot2<-ggplot(DATA1, aes(x=Date,y=Temperature))+
geom_line()+
ylab("Temperature [C]") +
scale_x_date(labels = date_format("%m/%y"),breaks = date_breaks("months"))+
theme_minimal()+
theme(panel.background=element_rect(fill = "white") )
plot3<-ggplot(DATA1, aes(x=Date,y=WindSpeed))+
geom_line()+
ylab("Wind Speed [km/h]") +
scale_x_date(labels = date_format("%m/%y"),breaks = date_breaks("months"))+
theme_minimal()+
theme(panel.background=element_rect(fill = "white") )
grid.arrange(plot1, plot2, plot3, nrow=3)
组合图如下所示:
您可以为此简单地使用分面。首先,您必须进行一些数据处理:
library(tidyr)
new_data = gather(DATA1, variable, value, Load, Temperature, WindSpeed)
这会将 Load
、Temperature
和 Windspeed
中的所有数据收集到一个大列中 (value
)。此外,还会创建一个额外的列 (variable
),用于指定向量中的哪个值属于哪个变量。
之后你可以绘制数据:
ggplot(new_data) + geom_line(aes(x = Date, y = value)) +
facet_wrap(~ variable, scales = 'free_y', ncol = 1)
现在 ggplot2 将处理所有繁重的工作。
ps 如果你提出问题 reproducible,我可以让我的答案可重现。