在我的geom_histogram栏之间添加空格 - 而不是barplot

Mar*_*opf 6 r ggplot2

假设我想制作直方图

所以我使用以下代码

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
Run Code Online (Sandbox Code Playgroud)

情节

如何将我的色谱柱分开,使整个物体更加悦目?

我试过这个,但它不知何故不起作用:

在ggplot2中的栏之间添加空格

谢谢

Koe*_*enV 10

你可以设置线的颜色与该柱状图的col参数,并填充颜色fill参数。这并不是真正在条形之间增加空间,而是使它们在视觉上不同。

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))
Run Code Online (Sandbox Code Playgroud)

产生这个图:

在此处输入图片说明

请让我知道这是否是您想要的。

  • 当然,与问题的标题无关 (5认同)

Tam*_*agy 5

如果您想增加空间,例如以指示值是离散的,要做的一件事是将直方图绘制为条形图。在这种情况下,您必须自己汇总数据,并使用geom_col()代替geom_histogram()。如果要进一步增加空间,可以使用该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")
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明