如何绘制矩阵形式的数据的散点图

huangapple go评论56阅读模式
英文:

How to plot scatter plot for a data in matrix form

问题

我有矩阵形式的数据。我有两个这样的矩阵,想要将它们绘制在一起进行比较。

类型1:

  A B C D
A 1 2 3 4
B 2 1 7 8
C 3 7 1 9
D 4 8 9 1
类型2

  A  B  C  D
A 1  12 13 14
B 12 1  17 18
C 13 17 1  19
D 14 18 19 1

我想将类型1作为x轴,类型2作为y轴。我如何使用R绘制这些数据的散点图呢?

非常感谢!

英文:

I have data in a matrix form. I have two such matrices and want to plot it together for comparison purposes.

Type1:

  A B C D
A 1 2 3 4
B 2 1 7 8
C 3 7 1 9
D 4 8 9 1
Type2

  A  B  C  D
A 1  12 13 14
B 12 1  17 18
C 13 17 1  19
D 14 18 19 1

I want to keep type 1 as x-axis and type 2 as y-axis. How can I draw a scatter plot for this data using R?

Thanks a lot in advance!

答案1

得分: 1

以下是您要翻译的内容:

您可以首先将矩阵值存储到数据框中。您可以使用函数 c(matrix1) 将矩阵转换为向量,然后将此向量存储到数据框中。

然后,您可以绘制每个数据框的新变量相互对比。

### 导入库
library(ggplot2)

### 模拟数据
df <- data.frame(
  coordinate=c("AA", "AB", "AC", "AD", 
               "BA", "BB", "BC", "BD", 
               "CA", "CB", "CC", "CD",
               "DA", "DB", "DC", "DD"),
  matrix1=c(1, 2, 3, 4, 2, 1, 7, 8, 3, 7, 1, 9, 4, 8, 9, 1),
  matrix2=c(1, 12, 13, 14, 12, 1, 17, 18, 13, 17, 1, 19, 14, 18, 19, 1))

### 显示绘图
ggplot(data=df, aes(x=matrix1, y=matrix2)) + 
  geom_point()  + 
  geom_line() + 
  scale_x_continuous(limits=c(0, 20), breaks=seq(0, 20, 1)) + 
  scale_y_continuous(limits=c(0, 20), breaks=seq(0, 20, 1))

有时连接点没有意义,如果是这样,您可以删除 geom_line() 行。

在您的第二个问题之后,如果您想要添加标签,您有很多选项。您可以使用以下代码找到两个选项,要么使用 ggrepel 库中的 geom_text_repel,要么简单地使用 ggplot2 库中的 geom_text,在其中调整 widthheight 参数。

### 更新具有相同坐标的标签组
df$matrixboth <- paste(df$matrix1, df$matrix2)

### 显示绘图
ggplot(data=df, aes(x=matrix1, y=matrix2, label=coordinate)) + 
  geom_point()  + 
  geom_line(color="#6aa6e7", size=1) + 
  scale_x_continuous(limits=c(0, 20), breaks=seq(0, 20, 1)) + 
  scale_y_continuous(limits=c(0, 20), breaks=seq(0, 20, 1)) + 
  # geom_text(position=position_jitter(width=0.6, height=0.6, aes(color=matrixboth)) + 
  ggrepel::geom_text_repel(aes(label=coordinate, color=matrixboth)) + 
  theme(legend.position="none")

geom_text_repel

geom_text

英文:

You can first store your matrix values into a dataframe. You can use the function c(matrix1) to convert your matrix into a vector then storing this vector into a dataframe.

Then you can plot each dataframe new variable against each other.

### Import library
library(ggplot2)

### Simulating data
df &lt;- data.frame(
  coordinate=c(&quot;AA&quot;, &quot;AB&quot;, &quot;AC&quot;, &quot;AD&quot;, 
               &quot;BA&quot;, &quot;BB&quot;, &quot;BC&quot;, &quot;BD&quot;, 
               &quot;CA&quot;, &quot;CB&quot;, &quot;CC&quot;, &quot;CD&quot;,
               &quot;DA&quot;, &quot;DB&quot;, &quot;DC&quot;, &quot;DD&quot;),
  matrix1=c(1, 2, 3, 4, 2, 1, 7, 8, 3, 7, 1, 9, 4, 8, 9, 1),
  matrix2=c(1, 12, 13, 14, 12, 1, 17, 18, 13, 17, 1, 19, 14, 18, 19, 1))

### Display plot
ggplot(data=df, aes(x=matrix1, y=matrix2)) + 
  geom_point()  + 
  geom_line() + 
  scale_x_continuous(limits=c(0, 20), breaks=seq(0, 20, 1)) + 
  scale_y_continuous(limits=c(0, 20), breaks=seq(0, 20, 1))

Sometimes it makes no sense to link the dots, if so, you can remove the geom_line() row.

如何绘制矩阵形式的数据的散点图

Following your second question, if you want to add labels, you have many options. You can find two options with the following code, by either using geom_text_repel from ggrepel library or simply geom_text from ggplot2 library where you play with width and height arguments.

### Update group of labels with the same coordinates
df$matrixboth &lt;- paste(df$matrix1, df$matrix2)

### Display plot
ggplot(data=df, aes(x=matrix1, y=matrix2, label=coordinate)) + 
  geom_point()  + 
  geom_line(color=&quot;#6aa6e7&quot;, size=1) + 
  scale_x_continuous(limits=c(0, 20), breaks=seq(0, 20, 1)) + 
  scale_y_continuous(limits=c(0, 20), breaks=seq(0, 20, 1)) + 
  # geom_text(position=position_jitter(width=0.6, height=0.6, aes(color=matrixboth)) + 
  ggrepel::geom_text_repel(aes(label=coordinate, color=matrixboth)) + 
  theme(legend.position=&quot;none&quot;)

geom_text_repel

如何绘制矩阵形式的数据的散点图

geom_text

如何绘制矩阵形式的数据的散点图

huangapple
  • 本文由 发表于 2023年5月25日 23:13:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76333848.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定