R语言 关于图上某些特定标签的问题

5anewei6  于 2023-02-06  发布在  其他
关注(0)|答案(1)|浏览(211)

我有一个地块的代码,我试图添加特定的标签。
数据代码如下:

CombinedMetricScore<-c("zero", "5", "10", "15", "20", "25", "30", "35", "40", 
                                    "45", "50", "60", "M11", "MICKEY", "MEANING", "MICKEYTWO", 
                                    "MICKEYTHREE", "MIKE", "PASTA", "MCIDandPASS", 
                                    "MICKDorPASS", "MIKEDOORPASS", "WOMAC20andPASS" ,"Ideal")
FalsePositiveRate<-c( 0, 0.05, 0.08, 0.12, 0.2, 0.28, 0.19, 0.5, 0.6, 0.7, 0.8, 0.94,
                      0.11, 0.28, 0.07, 0.5, 0.08, 0.28, 0.04, 0.3, 0.03, 0.03, 0.22, 1 )
TruePositiveRate<-c(0, 0.31, 0.35, 0.46, 0.69, 0.73, 0.59, 0.92, 0.92, 0.96, 1, 1, 
                    0.46, 0.73, 0.42, 0.88, 0.35, 0.73, 0.46, 0.73, 0.46, 0.46, 0.69, 1)
ScoreOrMetric<-c("Metric", "Score", "Score", "Score", "Score", "Score", "Score", "Score", "Score", 
                  "Score", "Score", "Score", "Metric", "Metric", "Metric", "Metric", 
                  "Metric", "Metric", "Metric", "Metric", 
                  "Metric", "Score", "Score", "Metric" )

COMBINEDSCORETABLE<-data.frame(CombinedMetricScore, FalsePositiveRate, TruePositiveRate, ScoreOrMetric)

地块代码如下:

ggplot(COMBINEDSCORETABLE, aes(x = FalsePositiveRate, y = TruePositiveRate, color = ScoreOrMetric)) + 
  geom_abline(slope = 1, intercept = .5, lwd = 1.5, color = "grey") +
  geom_point(size =2, alpha = .8) + 
  coord_cartesian(xlim=c(0,1), ylim=c(0, 1)) +
  coord_fixed() +
  geom_text_repel(label = ifelse(TruePositiveRate > .44 + FalsePositiveRate,
                                 yes = CombinedMetricScore, no = ""), 
                  box.padding = 0.5)

问题:我想为以下两个点“5”、“45”添加标签,但不知道如何将其添加到现有地块代码中。

ogq8wdun

ogq8wdun1#

我们可以在ifelse逻辑中使用一个|(“OR”),不过一般来说,我建议只将需要的数据传递给geom_text_repel,而不是所有数据(大多数数据都有""),所以试试这个:

ggplot(COMBINEDSCORETABLE, aes(x = FalsePositiveRate, y = TruePositiveRate, color = ScoreOrMetric)) + 
  geom_abline(slope = 1, intercept = .5, lwd = 1.5, color = "grey") +
  geom_point(size =2, alpha = .8) + 
  coord_cartesian(xlim=c(0,1), ylim=c(0, 1)) +
  coord_fixed() +
  ggrepel::geom_text_repel(
    aes(label = CombinedMetricScore),
    box.padding = 0.5,
    data = ~ subset(., TruePositiveRate > (0.44 + FalsePositiveRate) | CombinedMetricScore %in% c("5", "45")))

相关问题