Adding space between my lines geom_histogram-not barplot

Let's say I want to make a histogram

So I am using the following code

v100<-c(runif(100))

v100
library(ggplot2)
private_plot<-ggplot()+aes(v100)+geom_histogram(binwidth = (0.1),boundary=0
)+scale_x_continuous(breaks=seq(0,1,0.1), lim=c(0,1))
private_plot

      

plot

How can I separate the speakers to make it all more pleasing to the eye?

I tried this, but it somehow doesn't work:

Adding space between bars in ggplot2

thank

+6


source to share


3 answers


You can set the line color of histograms with parameter col

and color color with parameter fill

. It doesn't really add space between the columns, but it does make them visually distinct.

library(ggplot2)
set.seed(9876)
v100<-c(runif(100))

### use "col="grey" to set the line color
ggplot() +
  aes(v100) +
  geom_histogram(binwidth = 0.1, fill="black", col="grey") +
  scale_x_continuous(breaks = seq(0,1,0.1), lim = c(0,1))

      

Yielding to this schedule:



enter image description here

Please let me know if you want this.

+3


source


Just use the color

and parameters fill

to differentiate between the body and border of the trays:

library(ggplot2)
set.seed(1234)
df <- data.frame(sex=factor(rep(c("F", "M"), each=200)),
                 weight=round(c(rnorm(200, mean=55, sd=5), rnorm(200, mean=65, sd=5))))

ggplot(df, aes(x=weight)) + 
geom_histogram(color="black", fill="white")

      



enter image description here

0


source


If you want to increase the space, for example, to indicate that the values ​​are discrete, you need to make one bar chart. In this case, you have to sum the data yourself and use geom_col()

instead geom_histogram()

. If you want to increase the space even more, you can use the parameter width

.

library(tidyverse)
lambda <- 1:6

pois_bar <- 
    map(lambda, ~rpois(1e5, .x)) %>% 
    set_names(lambda) %>% 
    as_tibble() %>% 
    gather(lambda, value, convert = TRUE) %>% 
    count(lambda, value)

pois_bar %>% 
    ggplot() +
    aes(x = value, y = n) +
    geom_col(width = .5) +
    facet_wrap(~lambda, scales = "free", labeller = "label_both")

      

enter image description here

0


source







All Articles