如何在ggplot2中的轴标签下方添加一条线?

时间:2019-01-02 10:15:49

标签: r

如何在轴标签下添加一条线,就像所附图片中50,100和200轴标签下有一条线一样? enter image description here

2 个答案:

答案 0 :(得分:1)

示例(不包含GGPLOT2)

data("mtcars")
counts <- table(mtcars$gear)
barplot(counts, main="Car Distribution", 
        xlab="Number of Gears")
axis(1, # Put 1 for X-axis, 2 for Y-axis
     at=c(0, 5), #Limit of line
     col="red", 
     line=2.5, # how much gap you need between line and X-axis
     labels=rep("",2), # remove line labels
     lwd=2,
     lwd.ticks=0) # remove ticks

plt

多行,只需将另一个axis命令附加为-

data("mtcars")
counts <- table(mtcars$gear)
barplot(counts, main="Car Distribution", 
        xlab="Number of Gears")
axis(1, # Put 1 for X-axis, 2 for Y-axis
     at=c(0, 2.5), #Limit of line
     col="red", 
     line=2.5, # how much gap you need between line and X-axis
     labels=rep("",2), # remove line labels
     lwd=2,
     lwd.ticks=0) # remove ticks

axis(1, # Put 1 for X-axis, 2 for Y-axis
     at=3+c(0, 2.5), #Limit of line
     col="blue", 
     line=2.5, # how much gap you need between line and X-axis
     labels=rep("",2), # remove line labels
     lwd=2,
     lwd.ticks=0) # remove ticks

plt2

答案 1 :(得分:0)

也许facet_wrap中的ggplot2是一个起点:

library(magrittr)
library(ggplot2)

mtcars %>%
  dplyr::mutate(hp_200 = ifelse(hp > 200, "hp > 200", "hp <= 200")) %>%
  ggplot(aes(x = hp)) +
  geom_histogram(binwidth = 20) +
  facet_wrap(~ hp_200, scales = "free_x", strip.position = "bottom") +
  ggthemes::theme_hc()

reprex package(v0.2.1)于2019-01-02创建