英文:
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
,在其中调整 width
和 height
参数。
### 更新具有相同坐标的标签组
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 <- 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))
### 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 <- paste(df$matrix1, df$matrix2)
### Display plot
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论