如何绘制三个参数与一个组的关系?

问题描述 投票:0回答:1

我有一个数据框如下:

GR  CYT EMT PRO
High    8   5   8
Low 8   2   2
High    6   2   10
High    5   7   4
Low 7   2   1
Low 5   10  4
High    6   3   5
Low 8   1   10
High    4   7   5
High    2   3   8
Low 3   2   7
Low 5   7   1
High    7   7   2
High    5   1   3

我想使用 R 绘制图中的所有参数。我想检查三个参数 CYT、EMT、PRO 与 GR 的关系。

df <- data.frame(
  GR = c("High", "Low", "High", "High", "Low", "Low", "High", "Low", "High", "High", "Low", "Low", "High", "High"),
  CYT = c(8, 8, 6, 5, 7, 5, 6, 8, 4, 2, 3, 5, 7, 5),
  EMT = c(5, 2, 2, 7, 2, 10, 3, 1, 7, 3, 2, 7, 7, 1),
  PRO = c(8, 2, 10, 4, 1, 4, 5, 10, 5, 8, 7, 1, 7, 3)
)

我尝试过类似以下的方法:

# Melt the data to long format for better plotting
df_long <- reshape2::melt(df, id.vars = "GR")

# Create a grouped bar plot
ggplot(df_long, aes(x = variable, y = value, fill = GR)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(title = "Relationship of Parameters with GR",
       x = "Parameter",
       y = "Value") +
  theme_minimal()

还有其他方式来表示数据吗?

r ggplot2 plot multiple-instances
1个回答
0
投票

您可以使用箱线图来显示组之间的关系。 通过将级别从 1 更改为 2 和 3,查看各组的平均值如何变化。 该图经常用于假设检验

df_long <- reshape2::melt(df, id.vars = "GR")

ggplot(df_long, aes(x= 变量, y = 值, col = GR)) + geom_boxplot()

您还可以研究 H-L 和小组之间的互动。

ggplot(df_long, aes(col= variable, y = value, x = GR)) + geom_boxplot()

© www.soinside.com 2019 - 2024. All rights reserved.