1

I want to overly a scatter plot as below, but the first scatter plot is gone after the second plot is drawn though I wrote par(new = TRUE). Does anybody know how to do?

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

File 1:

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

File 2:

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

Thank you in advance!

0

1 Answer 1

1

par is used for basic R plot. For ggplot2, you can use following code

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)

enter image description here

If you want to overlay one scatter plot one the another then use

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))

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.