删除刻度线,但保留轴标签ggplot2 R

时间:2020-11-11 16:22:49

标签: r ggplot2

假设我有以下data.frame:

    structure(list(x = c("item_1", "item_2", "item_3", "item_4"), 
        y = c("location_1", "location_2", "location_3", "location_4"
        )), class = "data.frame", row.names = c(NA, -4L))

########################
#       x          y
#1 item_1 location_1
#2 item_2 location_2
#3 item_3 location_3
#4 item_4 location_4

具有以下情节:

data %>% ggplot(aes(x,y)) + geom_point()

enter image description here

是否可以将所有四个标签保留在y轴上,但仅显示location_4和location_1的刻度线

1 个答案:

答案 0 :(得分:1)

是的。主题元素axis.ticks.y将采用矢量化颜色。

data %>% 
  ggplot(aes(x,y)) + 
  geom_point() +
  theme(axis.ticks.y = element_line(color = c("black", NA, NA, "black")),
        axis.ticks.length = unit(5, "points"))

enter image description here