簡體   English   中英

在 R 中使用 ggplot2 覆蓋散布 plot

[英]Overlying scatter plot using ggplot2 in R

我想過度分散 plot 如下,但是第一個分散 plot 在第二個 plot 被繪制之后消失了雖然我寫了 par(new =TRUE) 有人知道該怎么做嗎?

library(readr)
library(ggplot2)
library(dplyr)
library("data.table")

file <-"1.txt"
df <-fread(file,header=T,data.table=FALSE) 
mode(df[,2]) <- "numeric"
mode(df[,3]) <- "numeric"
colnames(df) <- c("ID","Number","Scale")

plot <- ggplot(df,aes(x=df[,3],y=df[,2]))
plot <- plot + geom_point(color="red",size=1)

plot <- plot + theme_bw()
plot <- plot + xlab("Scale")
plot <- plot + ylab("Number")
plot <- plot + theme(panel.border=element_blank(),axis.line=element_line(colour="black"),axis.text=element_text(size=8))
plot <- plot + coord_fixed() 
plot
par(new = TRUE) 
file2 <-"2.txt"
df <-fread(file2,header=T,data.table=FALSE) 
mode(df2[,2]) <- "numeric"
mode(df2[,3]) <- "numeric"
colnames(df2) <- c("ID","Number","Scale")

plot <- ggplot(df2,aes(x=df[,3],y=df[,2]))
plot <- plot + geom_point(color="black",size=1)

plot <- plot + theme_bw()
plot <- plot + xlab("Scale")
plot <- plot + ylab("Number")
plot <- plot + theme(panel.border=element_blank(),axis.line=element_line(colour="black"),axis.text=element_text(size=8))
plot <- plot + coord_fixed() 
plot

文件一:

ID  Number  Scale
1   0.317949    0.351713714
2   0.40617 0.451713593
3   0.224795    0.743477906
4   0.352782    0.171798387
5   0.41111 0.51543521
6   0.38464 0.27841593

文件二:

ID  Number  Scale
1   0.425495    0.498275794
2   0.317949    0.351713714
3   0.458778    0.435448291
4   0.225754    0.756472645
5   0.352782    0.171798387
6   0.41111 0.51543521

先感謝您!

par用於基本 R plot。 對於ggplot2 ,您可以使用以下代碼

library(ggpubr)
library(ggplot2)
library(dplyr)
library("data.table")

df1 <-read.table(text = "ID  Number  Scale
1   0.317949    0.351713714
2   0.40617 0.451713593
3   0.224795    0.743477906
4   0.352782    0.171798387
5   0.41111 0.51543521
6   0.38464 0.27841593", header = T)
 
plot1 <-ggplot(df1,aes(x=Scale, y=Number)) + 
  geom_point(color="red",size=1) + theme_bw() + 
  xlab("Scale") + ylab("Number")+ 
  theme(panel.border=element_blank(),axis.line=element_line(colour="black"),axis.text=element_text(size=8)) 

plot1

df2 <- read.table(text = "ID  Number  Scale
1   0.425495    0.498275794
2   0.317949    0.351713714
3   0.458778    0.435448291
4   0.225754    0.756472645
5   0.352782    0.171798387
6   0.41111 0.51543521", header = T)

plot2 <-ggplot(df2,aes(x=Scale, y=Number)) + 
  geom_point(color="black",size=1) + theme_bw() + 
  xlab("Scale") + ylab("Number")+ 
  theme(panel.border=element_blank(),axis.line=element_line(colour="black"),axis.text=element_text(size=8))

plot2
ggarrange(plot1, plot2)

在此處輸入圖像描述

如果你想覆蓋一個散點 plot 一個另一個然后使用

df1 <-read.table(text = "ID1  Number1  Scale1
1   0.317949    0.351713714
2   0.40617 0.451713593
3   0.224795    0.743477906
4   0.352782    0.171798387
5   0.41111 0.51543521
6   0.38464 0.27841593", header = T)
 
df2 <- read.table(text = "ID2  Number2  Scale2
1   0.425495    0.498275794
2   0.317949    0.351713714
3   0.458778    0.435448291
4   0.225754    0.756472645
5   0.352782    0.171798387
6   0.41111 0.51543521", header = T)

df <- cbind.data.frame(df1, df2)

ggplot(df,aes(x=Scale1, y=Number1)) + 
  geom_point(color="red",size=1) + 
  geom_point(aes(x=Scale2, y=Number2)) + theme_bw() + 
  xlab("Scale") + ylab("Number")+ 
  theme(panel.border=element_blank(),axis.line=element_line(colour="black"),axis.text=element_text(size=8))

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM