我有这样的数据
df<- structure(list(Number = 1:23, Value1 = c(0.054830335, 1.19531842,
3.27820329, 1.03530176, 5.77430976, 3.72944, -0.683513395, 0.029550239,
2.487922644, 0.533448117, 0.098825565, -1.089022938, 2.301631235,
-0.095666867, -1.359480317, -1.359480317, 1.089441628, 3.307589929,
4.67838434, 3.562761178, 2.630726653, 1.795107015, 2.616255192
), Value2 = c(-0.296874921, 1.491747294, 2.951219257, 1.258677675,
-8.68096591, 3.361029751, -1.824459195, -1.445827538, 1.889631269,
-15.47774216, 3.085461276, -1.078286963, 0.948056999, -2.109354753,
-1.36703068, -1.36703068, 1.074642842, 2.945589842, 3.757911793,
2.765225717, 2.44452491, 1.784451022, 1.158493893)), class = "data.frame", row.names = c(NA,
-23L))
我正在尝试制作一个点图(一种颜色代表 Value1 与 Number),以及一个带有 Value2 与 Number 的点图。然后以较大尺寸显示前 5 个值,以较大尺寸显示后 5 个值
我尝试像这样绘制它
df$Number <- factor(df$Number, levels = paste0("D", 1:23), ordered = TRUE)
ggplot(df, aes(x=Value1, y=Value2, color= Number)) +
geom_text()+
theme_classic()
我可以像这样绘制其中一个
ggplot(data = df, aes(x = Number, y = Value1))+
geom_point()
当谈到在同一情节上有第二个时,有点模糊。
我可以用这种方式把它们组合在一起
# wide to long format
plotDf <- gather(df, Group, Myvalue, -1)
# plot
ggplot(plotDf, aes(Number, Myvalue, col = Group)) +
geom_point()
我仍然不知道如何以更大的尺寸显示前 5 个值和以更大的尺寸显示后 5 个值
前 5 个和后 5 个我指的是这些
df
Number Value1 Value2
1 1 0.05483034 -0.2968749
2 2 1.19531842 1.4917473
3 3 3.27820329 2.9512193
4 4 1.03530176 1.2586777
5 5 5.77430976 -8.6809659
6 6 3.72944000 3.3610298
7 7 -0.68351339 -1.8244592
8 8 0.02955024 -1.4458275
9 9 2.48792264 1.8896313
10 10 0.53344812 -15.4777422
11 11 0.09882557 3.0854613
12 12 -1.08902294 -1.0782870
13 13 2.30163123 0.9480570
14 14 -0.09566687 -2.1093548
15 15 -1.35948032 -1.3670307
16 16 -1.35948032 -1.3670307
17 17 1.08944163 1.0746428
18 18 3.30758993 2.9455898
19 19 4.67838434 3.7579118
20 20 3.56276118 2.7652257
21 21 2.63072665 2.4445249
22 22 1.79510701 1.7844510
23 23 2.61625519 1.1584939
这是前 5 个
1 1 0.05483034 -0.2968749
2 2 1.19531842 1.4917473
3 3 3.27820329 2.9512193
4 4 1.03530176 1.2586777
5 5 5.77430976 -8.6809659
这是最后 5 个
19 19 4.67838434 3.7579118
20 20 3.56276118 2.7652257
21 21 2.63072665 2.4445249
22 22 1.79510701 1.7844510
23 23 2.61625519 1.1584939
最佳答案
使用原始数据(不带因子
):
ggplot(df, aes(Number, Value1, size = (Number <= 5 | Number > 18))) +
geom_point() +
geom_point(aes(y=Value2)) +
scale_size_manual(name = NULL, values = c("TRUE" = 2, "FALSE" = 0.5)) +
scale_x_continuous(breaks = function(z) do.call(seq, as.list(round(z,0))))
因为使用逻辑条件来确定size=
,所以为其分配的手动值需要与观察到的各个值的字符版本相对应,这当然是TRUE
和 FALSE
逻辑转换为 "TRUE"
和 "FALSE"
。我对 2
和 0.5
的选择是任意的。
如果需要,请随意使用 name="some name"
更好地命名图例。如果您不需要图例(这是有道理的),您可以使用
... +
scale_size_manual(guide = "none", values = c("TRUE" = 2, "FALSE" = 0.5))
相反。
如果您想区分 Value#
的点,另一种选择是在绘图之前将数据融合为长格式。
ggplot(reshape2::melt(df, "Number"),
aes(Number, value, color = variable,
size = (Number <= 5 | Number >= 18))) +
geom_point() +
scale_size_manual(guide = "none", values = c("TRUE" = 2, "FALSE" = 0.5))
可以使用 tidyr::pivot_longer
或 data.table::melt
获得类似的结果,请参阅 Reshaping data.frame from wide to long format .
关于r - 如何根据每一列制作点图并突出显示开头和结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74749940/