Pisit Nakjai
Pisit Nakjai

Reputation: 181

How to label minimum value in each x step axis in R (ggplot) library

I'm plotting ggplot with the geom_text_repel library. The x-axis will be individuals' ID, and the y-axis is continued value.

How can I add label the minimum y value in each step of the x-axis. this is my simple code.

dummy_plot = ggplot(data = summary_mmdata ,
                aes(x = factor(type),y = WER_Value,
                    linetype=factor(WER_Type),
                    shape = factor(method),
                    group =factor(WER_Type):factor(method)
                )) +


geom_line()+
  geom_point()+
  scale_y_log10()+
  scale_shape_discrete(name = "",
                       labels=c("Test_Distance"="D1","Test_DistanceV2"="D2T",
                                "Test_DistanceV2MAV"="D2M","Test_DistanceV2Skip2"="D2S",
                                "Test_HeatMap"="HM"))+

  ylab("AER")+
  xlab("")+
  geom_text_repel(
    data = subset(summary_mmdata, WER_Value == min(WER_Value)),
    aes(label = WER_Value),
    size = 5,
    box.padding = unit(0.35, "lines"),
    point.padding = unit(0.3, "lines")
  )

dummy_plot

I used the geom_text_repel library. It marks label only the minimum value. I would like all each step in the x-axis. Please advise me. enter image description here

Upvotes: 0

Views: 491

Answers (1)

Ahorn
Ahorn

Reputation: 3876

Here is a reproducible example with the mtcars dataset. I group the data input of the geom_text_regel and then keep the minimum values and override all other values with "", which produces no labels for those observations:

mtcars %>% 
  ggplot(aes(x = factor(gear), y = mpg))+
  geom_line()+
  geom_point()+
  scale_y_log10()+
  scale_shape_discrete(name = "",
                       labels=c("Test_Distance"="D1","Test_DistanceV2"="D2T",
                                "Test_DistanceV2MAV"="D2M","Test_DistanceV2Skip2"="D2S",
                                "Test_HeatMap"="HM"))+
  ylab("AER")+
  xlab("")+
  geom_text_repel(
    data = mtcars %>% group_by(gear) %>% mutate(label_var = if_else(mpg == min(mpg), as.character(min(mpg)), "")),
    aes(label = label_var),
    size = 5,
    box.padding = unit(0.35, "lines"),
    point.padding = unit(0.3, "lines")
  )


enter image description here

So in your case I think this should work:

geom_text_repel(
  data = summary_mmdata %>% group_by(type) %>% mutate(WER_VALUE = if_else(WER_VALUE == min(WER_VALUE), as.character(min(WER_VALUE)), "")),
  aes(label = label_var),
  size = 5,
  box.padding = unit(0.35, "lines"),
  point.padding = unit(0.3, "lines")
)

Upvotes: 1

Related Questions