r - 我需要使用 ggplot2 更改每个图形的网格高度和大小
问题描述
我是 R 编程的新手。我正在尝试使用多个图表创建一个网格,但我想更改图表的高度和宽度,因为图表不清楚
我正在了解情节,但我需要对其进行一些更改以使其有意义
plot_1 <- ggplot(data_airfares ) +
geom_point(aes(x= NEW, y = FARE ), size = 1,colour="blue") + ggtitle("New Flights vs Fare")
plot_2 <- ggplot(data_airfares )+
geom_point(aes(x= COUPON, y = FARE ), size = 1,colour="red") + ggtitle("Coupon vs Fare")
plot_3 <- ggplot(data_airfares )+
geom_point(aes(x= HI, y = FARE ), size = 1,colour="green")+ ggtitle("HI vs Fare")+
theme(axis.text.x = element_text(angle = 90))
plot_4 <- ggplot(data_airfares )+
geom_point(aes(x= S_INCOME, y = FARE ), size = 1,colour="red")+ ggtitle("S_Income vs Fare")+
theme(axis.text.x = element_text(angle = 90))
plot_5 <- ggplot(data_airfares )+
geom_point(aes(x= E_INCOME, y = FARE ), size = 1)+ ggtitle("Income(E)vs Fare")+
theme(axis.text.x = element_text(angle = 90))
plot_6 <- ggplot(data_airfares )+
geom_point(aes(x= S_POP, y = FARE ), size = 1)+ ggtitle("Start_City_Population vs Fare")+
theme(axis.text.x = element_text(angle = 90))
plot_7 <- ggplot(data_airfares )+
geom_point(aes(x= E_POP, y = FARE ), size = 1)+ ggtitle("End_City_Population vs Fare")+
theme(axis.text.x = element_text(angle = 90))
plot_8 <- ggplot(data_airfares )+
geom_point(aes(x= DISTANCE, y = FARE ), size = 1)+ggtitle("Distance vs Fare")+
theme(axis.text.x = element_text(angle = 90))
plot_9 <- ggplot(data_airfares )+
geom_point(aes(x= PAX, y = FARE ), size = 1)+ ggtitle("Pax vs Fare")+
theme(axis.text.x = element_text(angle = 90))
grid.arrange(plot_1, plot_2, plot_3, plot_4, plot_5, plot_6, plot_7, plot_8,
plot_9, nrow = 3)
使用ggplot2更改网格中每个图的高度和宽度
解决方案
我的建议是为 x 轴使用不同的刻度标签,并减小标题和轴的字体大小。
library(ggplot2)
library(gridExtra)
library(scales)
# Generate data for the example
n <- 300
data_airfares <- data.frame(NEW = rbinom(n,4,0.25)+1,
FARE=runif(n,0,400),
COUPON=runif(n,1,2),
HI=runif(n,0,10000),
S_INCOME=runif(n,0,40000),
E_INCOME=runif(n,0,40000),
S_POP=runif(n,0,7500000),
E_POP=runif(n,0,7500000),
DISTANCE=runif(n,0,3000),
PAX=runif(n,0,70000))
# Create plots using a loop
plots <- NULL
var_names <- names(data_airfares)[-2]
for (k in 1:length(var_names)) {
var_name <- var_names[k]
df <- data_airfares[, c("FARE", var_name)]
p <- ggplot(data=df, aes_string(x=var_name, y="FARE")) +
geom_point(size = 1,colour="blue") +
ggtitle("New Flights vs Fare") +
theme(plot.title = element_text(size = 10), axis.title = element_text(size = 10))
if (var_name %in% c("S_POP","E_POP")) {
plots[[k]] <- p +
scale_x_continuous(labels = comma_format(accuracy=1, scale=10^(-6), suffix = "M"))
} else if (var_name %in% c("NEW","COUPON")) {
plots[[k]] <- p
} else {
plots[[k]] <- p +
scale_x_continuous(labels = comma_format(accuracy=1, scale=10^(-3), suffix = "K"))
}
}
grid.arrange(grobs=plots, nrow = 3)
推荐阅读
- java - 如何调试 java.sql.SQLException:无法解析连接标识符
- php - 无法使用 imagick 保存 webp 格式 - 已解决
- python-3.x - 在下面的代码中使用 'while not' & != 的区别,使用 'while not' 可以正确执行代码,如下面的类型 2,而 != 它只是一个无限循环?
- python - Cythonized pyparser not working properly, getting wrong argument count to functions
- vue.js - Vuetify v-select 与图像
- flutter - 在flutter中过滤jsonData
- mongodb - Stitch MongoDB updateOne 不更新
- ruby-on-rails - 任何示例如何在 Rails 中使用延迟作业进行大规模更新
- mongodb - mongo 连接器中的 mongotranslate 和模式聚合
- javascript - 如何通过json服务在node中构建odata服务