简体   繁体   中英

geom_col define the width of the bars

I have a data frame like a (but with much more line) and I would like to produce à bar plot or a histogram.

a <- structure(list(X.step. = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L), i.proba.transmission = c(0.1, 
0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 0.1, 0.2, 0.3, 0.4, 
0.5, 0.6, 0.7, 0.8, 0.9, 1, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 
0.8, 0.9, 1, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 
0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 0.1, 0.2, 0.3, 
0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 
0.7, 0.8, 0.9, 1, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 
1, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 0.1, 0.2, 
0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1), med.contamination = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1), med.s = c(499, 
499, 499, 499, 499, 499, 499, 499, 499, 499, 499, 499, 499, 499, 
499, 499, 499, 499, 499, 499, 499, 499, 499, 499, 499, 499, 499, 
498.5, 498, 498, 499, 499, 499, 499, 498, 498.5, 498, 498, 497.5, 
498, 499, 499, 499, 498, 498, 498, 498, 498, 497, 497, 499, 499, 
498, 498, 498, 498, 498, 497, 497, 497, 499, 499, 498, 498, 498, 
497.5, 497, 497, 496, 496, 499, 499, 498, 498, 497, 497, 497, 
496, 495.5, 495.5, 499, 499, 498, 497, 497, 497, 496.5, 495, 
495.5, 494, 499, 498, 497, 497, 496, 496, 496, 495, 495, 494), 
    med.Ir = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1.5, 2, 2, 1, 1, 1, 1, 2, 
    1.5, 2, 2, 2.5, 2, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 1, 1, 2, 
    2, 2, 2, 2, 3, 3, 3, 1, 1, 2, 2, 2, 2.5, 3, 3, 4, 4, 1, 1, 
    2, 2, 3, 3, 3, 4, 4.5, 4.5, 1, 1, 2, 3, 3, 3, 3.5, 5, 4.5, 
    6, 1, 2, 3, 3, 4, 4, 4, 5, 5, 6), med.Inr = c(0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -100L), groups = structure(list(X.step. = 0:9, 
    .rows = list(1:10, 11:20, 21:30, 31:40, 41:50, 51:60, 61:70, 
        71:80, 81:90, 91:100)), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE))

I have tried

# Histograme des contamination
ggplot(data = small.df)+
  geom_col(aes(x = X.step., y = med.contamination, fill = i.proba.transmission),
           binwidth = 0.2)+
  xlim(0,300)+
  theme_light()

and there is one bar for one X.step. ... I would like to merge 5 or 10 steps. 用 R 和 ggplot 生成的直方图

Try this. I create new groups combining five X.step. s using simple integer division. Next I sum med.contamination over the new groups:

library(ggplot2)
library(dplyr)

small.df %>%
  ungroup() %>%
  # Aggregate to groups of length 5 Steps using integer division "%/%"
  # First Group = 0 to 4, second = 5 to 9, ...
  mutate(X.step.1 = 5 * (X.step. %/% 5)) %>%
  # Aggregate med.contamination for new groups by i.proba.transmission
  count(X.step.1, i.proba.transmission, wt = med.contamination, name = "med.contamination") %>% 
  ggplot()+
  geom_col(aes(x = factor(X.step.1), y = med.contamination, fill = i.proba.transmission))+
  #xlim(0,300)+
  theme_light()

Created on 2020-03-31 by the reprex package (v0.3.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM