babylinguist
babylinguist

Reputation: 414

Remove horizontal line from geom_histogram in ggplot2

I would like to plot a histogram with some text off to the righthand side, but the x axis (or possibly the lower part of the histogram) extends out to the text. How can I remove this line?

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

Upvotes: 0

Views: 1071

Answers (1)

B Williams
B Williams

Reputation: 2050

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

Upvotes: 1

Related Questions