作图

R可视化——ggplot2如何实现同一类型图共用同一X轴?

2022-08-05  本文已影响0人  科研那点事儿

第一种方式

    主要通过拼图方式实现,将所有图形按照同一列进行排列,而且在绘图过程中需要去除上面几幅图的x轴刻度、标签及横线,具体代码如下:

rm(list=ls())
#设置工作环境
setwd("D:\\桌面")

#安装包
# install.packages("ggplot2")
# install.packages("ggprism")
#加载包
library(ggplot2)
library(ggprism)
#加载数据
df <- read.table(file="data.txt",sep="\t",header=T,check.names=FALSE)

#############第一种方式########
#Jan
p1<-ggplot(df)+
  geom_line(aes(date, Jan),size=0.8,color="red")+
  theme_prism(palette = "candy_soft",#主题设置
              base_fontface = "plain", 
              base_family = "serif", 
              base_size = 16,  
              base_line_size = 0.8, 
              axis_text_angle = 45)+
  scale_x_continuous(breaks=seq(1,31, 3))+#设置X轴标签范围及间隔
  theme(axis.ticks.x = element_blank(),#去除x轴刻度
        axis.text.x = element_blank(),#去除x轴标签
        axis.line.x = element_blank())+#去除x轴横线
  labs(title = "Jan", # 定义主标题
       x = NULL, # 定义x轴文本
       y = "Value")# 定义y轴文本
#Feb
p2<-ggplot(df)+
  geom_line(aes(date, Feb),size=0.8,color="green")+
  theme_prism(palette = "candy_soft",#主题设置
              base_fontface = "plain", 
              base_family = "serif", 
              base_size = 16,  
              base_line_size = 0.8, 
              axis_text_angle = 45)+
  scale_x_continuous(breaks=seq(1,31, 3))+#设置X轴标签范围及间隔
  theme(axis.ticks.x = element_blank(),
        axis.text.x = element_blank(),
        axis.line.x = element_blank())+
  labs(title = "Feb", # 定义主标题
       x = NULL, # 定义x轴文本
       y = "Value")# 定义y轴文本
#Mar
p3<-ggplot(df)+
  geom_line(aes(date, Mar),size=0.8,color="blue")+
  theme_prism(palette = "candy_soft",#主题设置
              base_fontface = "plain", 
              base_family = "serif", 
              base_size = 16,  
              base_line_size = 0.8, 
              axis_text_angle = 45)+
  scale_x_continuous(breaks=seq(1,31, 3))+#设置X轴标签范围及间隔
  theme(axis.ticks.x = element_blank(),
        axis.text.x = element_blank(),
        axis.line.x = element_blank())+
  labs(title = "Mar", # 定义主标题
       x = NULL, # 定义x轴文本
       y = "Value")# 定义y轴文本
#Apr
p4<-ggplot(df)+
  geom_line(aes(date, Apr),size=0.8,color="yellow")+
  theme_prism(palette = "candy_soft",#主题设置
              base_fontface = "plain", 
              base_family = "serif", 
              base_size = 16,  
              base_line_size = 0.8, 
              axis_text_angle = 45)+
  scale_x_continuous(breaks=seq(1,31, 3))+#设置X轴标签范围及间隔
  labs(title = "Apr", # 定义主标题
       x = 'Date', # 定义x轴文本
       y = "Value")# 定义y轴文本

#拼图
library(cowplot)
plot_grid(p1,p2,p3,p4,ncol=1)
image.png

第二种方式

    主要通过ggplot2包中的分面函数facet_wrap()进行绘制,代码如下:

##########第二种方式###########
library(reshape2)
library(tidyverse)
#处理数据
data <- df %>% melt(df,id.vars = c("date"),
                    measure.vars = c('Jan','Feb','Mar','Apr'),
                    variable.name = c('Mon'),
                    value.name = 'value')
#绘图
ggplot(data,aes(date, value,color=Mon))+
  geom_line(size=0.8)+
  facet_wrap(~Mon,nrow = 4)+#按照单一变量进行分面
  theme_prism(palette = "candy_soft",#主题设置
              base_fontface = "plain", 
              base_family = "serif", 
              base_size = 16,  
              base_line_size = 0.8, 
              axis_text_angle = 45)+
  scale_x_continuous(breaks=seq(1,31, 3))+#设置X轴标签范围及间隔
  labs(title = "Run chart", # 定义主标题
       x = 'Date', # 定义x轴文本
       y = "Value")# 定义y轴文本
image.png

更多精彩欢迎大家关注微信公众号【科研后花园】!!!

上一篇下一篇

猜你喜欢

热点阅读