条件格式化ggplot标签不带显式重复所有刻度线向量

时间:2018-04-02 15:35:47

标签: r ggplot2

我想创建一个很好的情节来显示我选择的聚类数量,即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

如何在不明确编写整个矢量的情况下格式化xtick值11

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

1 个答案:

答案 0 :(得分:1)

您可以使用重复帖子中链接的技术,并为colourface参数提供单独的列表:

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

通过在theme选项中添加以下行,也可以使用相同的方法来更改刻度线颜色:

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