我对 R 完全陌生,我想使用 ggplot2 创建一个绘图。我的目标是绘制 x=CAO 和 Y=F 的散点图,我还需要数据点根据其在 RN 列中的名称具有特定的颜色。我可以手动完成,但我的实际数据集有数千个条目...
RN CAO F
<chr> <dbl> <chr>
1 HAWAIITE 52.7 4.64
2 HAWAIITE 53.9 3.75
3 MUGEARITE 53.8 3.59
4 MUGEARITE 53.4 3.45
5 BENMOREITE 53.5 4.2
6 BENMOREITE 53.3 2.00
7 TRACHYTE 53.2 3.68
8 HARZBURGITE, XENOLITH 52.2 4.82
9 WEHLERITE, XENOLITH 51.9 5.51
10 DUNITE, XENOLITH 54.0 4.40
谢谢您的帮助!
现在这是我的代码,我得到了散点图,但所有点都具有相同的颜色。
georoc %>%
select(RN,CAO,F)
ggplot(georoc, aes(x = CAO, y = F ,colour = "RN"))+
geom_point(size = 4, alpha = 0.8, stroke = 1, aes(color ="RN")) +
theme(legend.position= "none") +
labs(title = "CAO vs F",
x = "CAO", y = "F")
基于@stefan 和@JonSpring 的回答
library(ggplot2)
df <- data.frame(
RN = c("HAWAIITE", "HAWAIITE", "MUGEARITE", "MUGEARITE", "BENMOREITE", "BENMOREITE", "TRACHYTE", "HARZBURGITE, XENOLITH", "WEHLERITE, XENOLITH", "DUNITE, XENOLITH"),
CAO = c(52.7, 53.9, 53.8, 53.4, 53.5, 53.3, 53.2, 52.2, 51.9, 54.0),
F = c(4.64, 3.75, 3.59, 3.45, 4.2, 2.00, 3.68, 4.82, 5.51, 4.40)
)
ggplot(df, aes(x = CAO, y = F))+
geom_point(size = 4, alpha = 0.8, stroke = 1, aes(color = RN)) +
theme(legend.position= "none") +
labs(title = "CAO vs F",
x = "CAO", y = "F")
创建于 2024-06-07,使用 reprex v2.1.0