首页 > 解决方案 > 从 ggplot2 中的 geom_histogram 中删除水平线

问题描述

我想在右侧绘制一个带有一些文本的直方图,但是 x 轴(或者可能是直方图的下部)延伸到文本。我怎样才能删除这条线?

library(tibble)  
library(ggplot2)  
library(dplyr)

my_label <- "text"

p <- rnorm(10000) %>%
  as_tibble(.) %>%
  ggplot(., aes(x = value)) +
    geom_histogram(color = 'black', fill = 'grey70', bins = 30) +
    annotate(geom = 'text', x = 15, y = 1500, label = my_label,
             hjust = 1, size = 3, color = "#cc0000") +
    theme_void()

标签: rggplot2histogram

解决方案


A quick hack to rid your plot of the x-axis line

rnorm(10000) %>%
  as_tibble() -> dat

dat %>% 
  ggplot(aes(value)) +
  geom_histogram(color = 'black', fill = 'grey70') +
  annotate(geom = 'text', x = 15, y = 1500, label = my_label,
           hjust = 1, size = 3, color = "#cc0000") +
  theme_void() +
  geom_histogram(data = tibble(value=1:15), color = 'white')

推荐阅读