-1

I want to create a nice plot to show the number of clusters I choose, which is 11.

RS <- structure(list(k = 2:24, value = c(0.144146119842721, 0.222206168029977, 
0.291678571330934, 0.358919047522653, 0.426695606507329, 0.483301229692586, 
0.533728497594114, 0.580550588772305, 0.588601124711909, 0.635271587964058, 
0.650036125839732, 0.662220837971202, 0.675275766505185, 0.685226878333768, 
0.696300002606587, 0.705498413223437, 0.709250552710995, 0.716976400569355, 
0.72489699345261, 0.735887738035583, 0.741940077625176, 0.742248532545676, 
0.75185453741879)), .Names = c("k", "value"), row.names = c(NA, 
-23L), class = "data.frame")


    ggplot(RS)  + 
ggtitle("Root-mean square standard deviation error") + 
geom_line(aes(x = k, y = value), size = 1) + 
geom_point(aes(x = k, y = value), size = 2) + 
geom_segment(aes(x = RS[RS$k == 11, "k"], xend =  RS[RS$k == 11, "k"], y = min(RS[ , "value"]), yend = RS[RS$k == 11, "value"]), linetype = 2)  + 
scale_x_continuous(breaks = c(2, 5, 10, 11, 15, 20)) + theme_classic() +         
theme(axis.text.y = element_text(size=14), axis.text.x = element_text(size = 14), axis.title.x = element_text(size = 14), axis.title.y = element_text(size = 14))

enter image description here

How can I format the xtick value 11 without explicitly writing the entire vector like in this solution?

ggplot(RS)  + 
        ggtitle("Root-mean square standard deviation error") +
        geom_line(aes(x = k, y = value), size = 1) +
        geom_point(aes(x = k, y = value), size = 2) +
        geom_segment(aes(x = RS[RS$k == 11, "k"],
                         xend =  RS[RS$k == 11, "k"],
                         y = min(RS[ , "value"]),
                         yend = RS[RS$k == 11, "value"]),
                     linetype = 2)  +
        scale_x_continuous(breaks = c(2, 5, 10, 11, 15, 20, 24)) +
        theme_classic() +
        theme(axis.text.y = element_text(size=14),
              axis.text.x = element_text(size = 14,
                                         face=ifelse(c(2,5,10,11,15,20, 24) == 11,'bold','plain'),
                                         color =ifelse(c(2,5,10,11,15,20, 24) == 11,'red','black')),
              axis.title.x = element_text(size = 14),
              axis.title.y = element_text(size = 14))  
Seymour
  • 3,104
  • 2
  • 22
  • 46
  • Possible duplicate of [Change color of specific tick in ggplot2](https://stackoverflow.com/questions/34841470/change-color-of-specific-tick-in-ggplot2) – Michael Harper Apr 02 '18 at 15:38
  • I already saw that question but: 1) It does not include bold formatting; 2) it is not feasible to write all the possible ticks – Seymour Apr 02 '18 at 15:43
  • If you have previously found an answer on stackoverflow which is similar, it is important that you mention this in the question. – Michael Harper Apr 02 '18 at 15:49

1 Answers1

1

You can use the technique linked in the duplicate post and provide separate lists for the colour and face arguments:

ggplot(RS)  + 
  ggtitle("Root-mean square standard deviation error") + 
  geom_line(aes(x = k, y = value), size = 1) + 
  geom_point(aes(x = k, y = value), size = 2) + 
  geom_segment(aes(x = RS[RS$k == 11, "k"], xend =  RS[RS$k == 11, "k"], y = min(RS[ , "value"]), yend = RS[RS$k == 11, "value"]), linetype = 2)  + 
  scale_x_continuous(breaks = c(2, 5, 10, 11, 15, 20)) + theme_classic() +         
  theme(axis.text = element_text(size=14), 
        axis.title = element_text(size = 14),
        axis.text.x = element_text(colour = c('black', 'black', 'black', 'red', 'black', 'black'),
                                   face = c('plain', 'plain','plain', 'bold', 'plain', 'plain')))

enter image description here

The same approach could also be used to change the tick colour by adding the following line into the theme options:

axis.ticks.x = element_line(colour = c('black', 'black', 'black', 'red', 'black', 'black')))

Michael Harper
  • 14,721
  • 2
  • 60
  • 84
  • You are right, I did not write properly the question. The point is how can I do this without writing the entire vector of all x ticks? – Seymour Apr 02 '18 at 15:56
  • That is quite a significant change to the question. Maybe better leaving this one as is and then opening another question. – Michael Harper Apr 02 '18 at 15:59