这是我的数据的样子。 长话短说,我想散点图 A 组的 Y 值与 B 组的相应 X 值,并可以选择按样本为其着色。
我做了很多像这样的数据绘制(有更多变量),但它总是在subset(data, Group=='A')
像or 这样的子集中subset(data, Group=='B')
。当我需要跨组绘图时,这是罕见的情况。
情节代码本身很简单:
ggplot(data, aes(x=X(B), y=Y(A), color=as.factor(Sample))) +
geom_point()
Run Code Online (Sandbox Code Playgroud)
我意识到我的 X(B) 和 Y(A) 不起作用;这只是为了说明目标。我还假设有一种方法可以生成仅包含 X(B) 和 Y(A) 值的新数据集;当然对任何方法都持开放态度,但我更喜欢在 GGPLOT 中处理实际的图表。这是我的可重复性数据集:
data <- structure(list(Group = c("A", "A", "A", "A", "A", "B", "B", "B",
"B", "B", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B"),
Sample = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), Point = c(1L, 2L, 3L, 4L,
5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L,
5L), X = c(-3.26, -3.26, -3.26, -3.26, -3.26, -2.3624, -2.3877,
-2.6475, -3.0975, -3.1393, -3.26, -3.26, -3.26, -3.26, -3.26,
-0.6476, -0.7056, -0.7367, -0.9883, -0.9003), Y = c(22.55,
22.02, 19.41, 10.92, 8.3, 4.14, 4.42, 9.92, 9.86, 7.57, 67.94,
66.92, 70.26, 63.37, 61.85, 11.79, 10.86, 12.96, 12.44, 11.69
)), class = "data.frame", row.names = c(NA, -20L))
Run Code Online (Sandbox Code Playgroud)
X(B)
您可以通过旋转得到和的等价物Y(A)
:
library(tidyverse)
pivoted <- data %>%
pivot_wider(names_from = Group, values_from = X:Y)
# data before pivoting
head(as_tibble(data))
# # A tibble: 6 x 5
# Group Sample Point X Y
# <chr> <int> <int> <dbl> <dbl>
# 1 A 1 1 -3.26 22.6
# 2 A 1 2 -3.26 22.0
# 3 A 1 3 -3.26 19.4
# 4 A 1 4 -3.26 10.9
# 5 A 1 5 -3.26 8.3
# 6 B 1 1 -2.36 4.14
# after pivoting
head(pivoted)
# # A tibble: 6 x 6
# Sample Point X_A X_B Y_A Y_B
# <int> <int> <dbl> <dbl> <dbl> <dbl>
# 1 1 1 -3.26 -2.36 22.6 4.14
# 2 1 2 -3.26 -2.39 22.0 4.42
# 3 1 3 -3.26 -2.65 19.4 9.92
# 4 1 4 -3.26 -3.10 10.9 9.86
# 5 1 5 -3.26 -3.14 8.3 7.57
# 6 2 1 -3.26 -0.648 67.9 11.8
Run Code Online (Sandbox Code Playgroud)
然后您可以轻松调整您的插图代码:
ggplot(pivoted, aes(x=X_B, y=Y_A, color=as.factor(Sample))) +
geom_point()
Run Code Online (Sandbox Code Playgroud)