Home > Software engineering >  Is there a way to create a histogram in R using ggplot so that only the vertical lines of the bins t
Is there a way to create a histogram in R using ggplot so that only the vertical lines of the bins t

Time:03-01

I am currently creating some histograms in R using ggplot that have many bins and a large data set (850 000 elements).

As a result the vertical lines of each bin are filling in the area under the histogram with the line colour due to there close proximity. I would ideally like this to be clear so I can plot another histogram on the same plot.

Ideally, I would like a histogram with the bin lines hidden where they overlap with another bin so It looks similar to a line plot.

Below is the ggplot code I'm using:

ggplot(df, aes(x=eev))  
  geom_histogram(binwidth = 18,color="black")  
  xlim(0,10000)  
  scale_y_log10(name="Log of Counts", labels = scales::comma)  
  xlab("Incident Energy in eV")

I can't really fiddle around with the bin size too much because I need the definition from the naarrow bins.

I've had a look through the ggplot documentation but can't find what I'm after.

Cheers

CodePudding user response:

Setting a transparent color like #ffffff00 (the last two digits setting opacity to zero) should do the trick. Control the fill colour (the inner of the histogram columns) with, well: fill.

Example:

data.frame(x = rnorm(10000)) %>%
    ggplot()   
    geom_histogram(aes(x), 
                   fill = 'blue', 
                   binwidth = .025,
                   col='#ffffff00'
                   )

Note that while you can increase the border thickness of the columns with the size argument, setting size = 0 does not fully remove the border.

CodePudding user response:

Maybe using hist to generate the values then plotting in ggplot:

library(ggplot2)
set.seed(1)
x = hist(rchisq(1000, df = 4), 100)
df = data.frame(
  x = rep(x$breaks, each=2), 
  y = c(0, rep(x$counts, each = 2), 0))
  
ggplot(df, aes(x,y))   
  geom_line(col='red')  
  geom_ribbon(aes(ymin = 0, ymax = y), fill='grey80')

enter image description here

  • Related