在ggplot2中进行绘制时,y轴被切断。
我试图通过使用par(mar)增加绘图区域,但是没有任何运气。
我对r很陌生,因此很可能在另一个问题中描述并解决了这个问题,但是我找不到一个可以解决我问题的问题。
非常感谢您的帮助。
library(tidyverse)
library(dplyr)
library(tidyr)
library(lubridate)
library(ggplot2)
library(grid)
df <- data.frame(date_time = seq(as.POSIXct("2018-01-01"),
as.POSIXct("2018-01-25"),
by=(60*60*24)), temp = temp <- runif(25, 10, 20),prec = prec <- runif(25, 0, 4), par = par <- runif(25, 1000, 1500))
par_plot <- ggplot(df, aes(date_time, par)) +
geom_line() +
theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
theme(axis.ticks.length = unit(.2, "cm"), axis.title.y = element_text(margin = margin(t = 0, r = 0, b = 0, l = -7, "mm")), axis.text.x = element_blank(), axis.title.x = element_blank()) +
scale_x_datetime(date_breaks = "3 days", expand = c(0.01, 0)) +
scale_y_continuous(expand = c(0, 0), limits = c(0, 1500))
temp_plot <- ggplot(df, aes(date_time, temp)) +
geom_line() +
theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
theme(axis.ticks.length = unit(.2, "cm"), axis.title.y = element_text(margin = margin(t = 0, r = 0, b = 0, l = -7, "mm")), axis.text.x = element_blank(), axis.title.x = element_blank()) +
scale_x_datetime(date_breaks = "3 days", expand = c(0.01, 0)) +
scale_y_continuous(expand = c(0, 0), limits = c(0, 20))
prec_plot <- ggplot(df, aes(date_time, prec)) +
geom_bar(stat="identity") +
theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
theme(axis.ticks.length = unit(.2, "cm"), axis.title.y = element_text(margin = margin(t = 0, r = 0, b = 0, l = -7, "mm")), axis.text.x = element_text(angle=90), axis.title.x = element_blank()) +
scale_x_datetime(date_breaks = "3 days", expand = c(0.01, 0)) +
scale_y_continuous(expand = c(0, 0), limits = c(0, 4))
grid.newpage()
par(mar = c(15, 15, 15, 15))
grid.draw(rbind(ggplotGrob(par_plot), ggplotGrob(temp_plot), ggplotGrob(prec_plot), size = "last"))