如果变量定义了如何绘制,如何在ggplot2的散点图中为值着色?

时间:2019-03-08 09:56:32

标签: r ggplot2

我有以下数据集:

      Species Country               IUCN_Area IUCN.Estimate Estimate.year
1 Reticulated   Kenya                    Embu             0          2018
2 Reticulated   Kenya Laikipia_Isiolo_Samburu          3043          2018
3 Reticulated   Kenya                Marsabit           625          2018
4 Reticulated   Kenya                    Meru           999          2018
5 Reticulated   Kenya                 Turkana             0          2018
6 Reticulated   Kenya              West Pokot             0          2018
  GEC_Stratum_Detect_Estimate UpperCI_detect LowerCI_detect
1                         130            277            -17
2                       16414          19919          12910
3                          57            347           -233
4                        4143           6232           2054
5                           0              0              0
6                           0              0              0

我想创建一个散点图,在x轴上具有“ IUCN估计”,在y轴上具有“ GEC_Stratum_Detect_Estimate”。然后,我想按类型(即“ IUCN”和“ GEC”)对点进行着色。但是,如果变量定义了轴,我该如何按点的颜色进行着色?我很确定必须要编写一个简单的代码,但是到目前为止,它一直困扰着我。我也尝试过重新设置数据集,但没有成功。这是情节代码:

ggplot(df, aes(x=IUCN.Estimate, y=GEC_Stratum_Detect_Estimate, shape=Species)) +
  geom_point() + 
  geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+
  theme_classic()

这是数据:

structure(list(Species = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Maasai", 
"Reticulated", "Southern"), class = "factor"), Country = structure(c(2L, 
2L, 2L, 2L, 2L, 2L), .Label = c("Botswana", "Kenya", "Tanzania"
), class = "factor"), IUCN_Area = structure(c(4L, 10L, 12L, 13L, 
23L, 25L), .Label = c("Burigi-Biharamulo", "Central District", 
"Chobe", "Embu", "Kajiado", "Katavi-Rukwa", "Kilifi", "Kitui", 
"Kwale", "Laikipia_Isiolo_Samburu", "Makueni/ Machakos", "Marsabit", 
"Meru", "Moremi GR", "Narok", "Ngamiland", "No IUCN Estimate", 
"Nxai and Makgadikgadi", "Ruahu-Rungwa-Kisigo", "Selous-Mikumi", 
"Taita Taveta", "Tana River", "Turkana", "Ugalla GR", "West Pokot"
), class = "factor"), IUCN.Estimate = c(0L, 3043L, 625L, 999L, 
0L, 0L), Estimate.year = c(2018L, 2018L, 2018L, 2018L, 2018L, 
2018L), GEC_Stratum_Detect_Estimate = c(130L, 16414L, 57L, 4143L, 
0L, 0L), UpperCI_detect = c(277L, 19919L, 347L, 6232L, 0L, 0L
), LowerCI_detect = c(-17L, 12910L, -233L, 2054L, 0L, 0L)), row.names = c(NA, 
6L), class = "data.frame")

谢谢。

1 个答案:

答案 0 :(得分:0)

以下内容将通过IUCN_Area为点着色:

library(ggplot2)
ggplot(df, aes(x=IUCN.Estimate, y=GEC_Stratum_Detect_Estimate, shape=Species)) +
  geom_point(aes(color=IUCN_Area)) + 
  geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+
  theme_classic()

以下是IUCN.Estimate。由于IUCN是数字值,因此ggplot会根据范围值默认设置颜色。如上所述,因子值由离散值着色。

library(ggplot2)
ggplot(df, aes(x=IUCN.Estimate, y=GEC_Stratum_Detect_Estimate, shape=Species)) +
  geom_point(aes(color=IUCN.Estimate)) + 
  geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+
  theme_classic()

由于以下IUCN和GEC都要求OP进行着色,因此可以做到这一点。如何解释它可能是另一回事。但是,可以将任何值赋予color变量。在这里,我将两个数字加在一起并设置为as.factor()。大概在大型数据集中,这些点的总和可能会识别出一组音符。

library(ggplot2)
ggplot(df, aes(x=IUCN.Estimate, y=GEC_Stratum_Detect_Estimate, shape=Species)) +
  geom_point(aes(color=as.factor(IUCN.Estimate+GEC_Stratum_Detect_Estimate))) + 
  geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+
  theme_classic()