JamesLancaster
JamesLancaster

Reputation: 59

Question regarding some specific labelling on plots

I have a code for a plot that I am trying to add specific labels for.

The data code is this:

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)

The plot code is this:

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)

Question: I want to add labels for the following 2 points "5", "45" but I don't know how to add it to my existing plot code.

Upvotes: 0

Views: 33

Answers (1)

r2evans
r2evans

Reputation: 160607

We can use an | ("OR") in your ifelse logic. In general, though, I recommend only passing the data you need to geom_text_repel instead of everything (most of which having ""), so try this:

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")))

ggplot with labels on "5" and "45"

Upvotes: 1

Related Questions