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

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

How to plot scatter plot for a data in matrix form

问题

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

  1. 类型1:
  2. A B C D
  3. A 1 2 3 4
  4. B 2 1 7 8
  5. C 3 7 1 9
  6. D 4 8 9 1
  1. 类型2
  2. A B C D
  3. A 1 12 13 14
  4. B 12 1 17 18
  5. C 13 17 1 19
  6. 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.

  1. Type1:
  2. A B C D
  3. A 1 2 3 4
  4. B 2 1 7 8
  5. C 3 7 1 9
  6. D 4 8 9 1
  1. Type2
  2. A B C D
  3. A 1 12 13 14
  4. B 12 1 17 18
  5. C 13 17 1 19
  6. 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) 将矩阵转换为向量,然后将此向量存储到数据框中。

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

  1. ### 导入库
  2. library(ggplot2)
  3. ### 模拟数据
  4. df <- data.frame(
  5. coordinate=c("AA", "AB", "AC", "AD",
  6. "BA", "BB", "BC", "BD",
  7. "CA", "CB", "CC", "CD",
  8. "DA", "DB", "DC", "DD"),
  9. matrix1=c(1, 2, 3, 4, 2, 1, 7, 8, 3, 7, 1, 9, 4, 8, 9, 1),
  10. matrix2=c(1, 12, 13, 14, 12, 1, 17, 18, 13, 17, 1, 19, 14, 18, 19, 1))
  11. ### 显示绘图
  12. ggplot(data=df, aes(x=matrix1, y=matrix2)) +
  13. geom_point() +
  14. geom_line() +
  15. scale_x_continuous(limits=c(0, 20), breaks=seq(0, 20, 1)) +
  16. scale_y_continuous(limits=c(0, 20), breaks=seq(0, 20, 1))

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

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

  1. ### 更新具有相同坐标的标签组
  2. df$matrixboth <- paste(df$matrix1, df$matrix2)
  3. ### 显示绘图
  4. ggplot(data=df, aes(x=matrix1, y=matrix2, label=coordinate)) +
  5. geom_point() +
  6. geom_line(color="#6aa6e7", size=1) +
  7. scale_x_continuous(limits=c(0, 20), breaks=seq(0, 20, 1)) +
  8. scale_y_continuous(limits=c(0, 20), breaks=seq(0, 20, 1)) +
  9. # geom_text(position=position_jitter(width=0.6, height=0.6, aes(color=matrixboth)) +
  10. ggrepel::geom_text_repel(aes(label=coordinate, color=matrixboth)) +
  11. 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.

  1. ### Import library
  2. library(ggplot2)
  3. ### Simulating data
  4. df &lt;- data.frame(
  5. coordinate=c(&quot;AA&quot;, &quot;AB&quot;, &quot;AC&quot;, &quot;AD&quot;,
  6. &quot;BA&quot;, &quot;BB&quot;, &quot;BC&quot;, &quot;BD&quot;,
  7. &quot;CA&quot;, &quot;CB&quot;, &quot;CC&quot;, &quot;CD&quot;,
  8. &quot;DA&quot;, &quot;DB&quot;, &quot;DC&quot;, &quot;DD&quot;),
  9. matrix1=c(1, 2, 3, 4, 2, 1, 7, 8, 3, 7, 1, 9, 4, 8, 9, 1),
  10. matrix2=c(1, 12, 13, 14, 12, 1, 17, 18, 13, 17, 1, 19, 14, 18, 19, 1))
  11. ### Display plot
  12. ggplot(data=df, aes(x=matrix1, y=matrix2)) +
  13. geom_point() +
  14. geom_line() +
  15. scale_x_continuous(limits=c(0, 20), breaks=seq(0, 20, 1)) +
  16. 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.

  1. ### Update group of labels with the same coordinates
  2. df$matrixboth &lt;- paste(df$matrix1, df$matrix2)
  3. ### Display plot
  4. ggplot(data=df, aes(x=matrix1, y=matrix2, label=coordinate)) +
  5. geom_point() +
  6. geom_line(color=&quot;#6aa6e7&quot;, size=1) +
  7. scale_x_continuous(limits=c(0, 20), breaks=seq(0, 20, 1)) +
  8. scale_y_continuous(limits=c(0, 20), breaks=seq(0, 20, 1)) +
  9. # geom_text(position=position_jitter(width=0.6, height=0.6, aes(color=matrixboth)) +
  10. ggrepel::geom_text_repel(aes(label=coordinate, color=matrixboth)) +
  11. 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:

确定