📜  R 中的并排散点图

📅  最后修改于: 2022-05-13 01:55:21.027000             🧑  作者: Mango

R 中的并排散点图

在本文中,我们将讨论如何在 R 编程语言中制作并排散点图。

方法一:使用par()函数

这里我们将使用 par()函数将帧划分为网格。

在这里,通过更改行和列值,我们可以在其中绘制图形。为了绘制并排图,我们将行作为 1,列作为编号。所需的地块。

R
# Define data-set columns
x1 <- c(31,13,25,31,16,12,23,43,12,22,45,32)
x2 <- c(12,23,43,12,22,45,32,31,13,25,31,16,21) 
  
# set the plotting area into a 1*2 array
par(mfrow = c(1,2))    
  
# Draw the two scatter chart using above datasets
plot(x1, col="red")
plot(x2, col="blue")


R
# Define data-set columns
x1 <- c(31,13,25,31,16)
x2 <- c(12,23,43,12,22,45,32) 
x3 <- c(234,123,210)
  
label1 <- c('geek','geek-i-knack',
            'technical-scipter',
            'content-writer',
            'problem-setter')
  
label2 <- c('sun','mon','tue',
            'wed','thur','fri','sat')
  
label3 <- c('solved','attempted','unsolved')
  
# Create data frame using above data column
data1 <- data.frame(x1,label1)
data2 <- data.frame(x2,label2)
data3 <- data.frame(x3,label3)
  
# set the plotting area into a 1*3 array
par(mfrow = c(1,3))
  
# import library ggplot2 and gridExtra
library(ggplot2)
library(gridExtra)
  
# Draw the three scatter chart using above datasets
plot1<-ggplot(data1, aes(x=label1, y=x1, group=1)) +
geom_point(color="green")
  
plot2<-ggplot(data2, aes(x=label2, y=x2, group=1)) +
geom_point()
  
plot3<-ggplot(data3, aes(x=label3, y=x3, group=1)) +
geom_point(color=c("red","yellow","blue"))
  
# Use grid.arrange to put plots in columns
grid.arrange(plot1, plot2, plot3, ncol=3)


输出:

方法二:使用ggplot2

在这里,我们将使用gridExtra 包含一个名为排列()的函数,用于根据需要排列图。

在这里,通过更改 nrow 和 ncol 值,我们可以在其中绘制图形。为了绘制并排图,我们将 nrow 保持为 1,ncol 保持为 no。所需的地块。

电阻

# Define data-set columns
x1 <- c(31,13,25,31,16)
x2 <- c(12,23,43,12,22,45,32) 
x3 <- c(234,123,210)
  
label1 <- c('geek','geek-i-knack',
            'technical-scipter',
            'content-writer',
            'problem-setter')
  
label2 <- c('sun','mon','tue',
            'wed','thur','fri','sat')
  
label3 <- c('solved','attempted','unsolved')
  
# Create data frame using above data column
data1 <- data.frame(x1,label1)
data2 <- data.frame(x2,label2)
data3 <- data.frame(x3,label3)
  
# set the plotting area into a 1*3 array
par(mfrow = c(1,3))
  
# import library ggplot2 and gridExtra
library(ggplot2)
library(gridExtra)
  
# Draw the three scatter chart using above datasets
plot1<-ggplot(data1, aes(x=label1, y=x1, group=1)) +
geom_point(color="green")
  
plot2<-ggplot(data2, aes(x=label2, y=x2, group=1)) +
geom_point()
  
plot3<-ggplot(data3, aes(x=label3, y=x3, group=1)) +
geom_point(color=c("red","yellow","blue"))
  
# Use grid.arrange to put plots in columns
grid.arrange(plot1, plot2, plot3, ncol=3)

输出: