geom_col定义条的宽度

问题描述 投票:1回答:1

我有一个像a的数据框(但线条更多),我想生成一个条形图或一个直方图。

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

我尝试过

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

并且一个X.step.有一个小节...我想合并5或10个步骤。histogram produced with R and ggplot

r ggplot2 bar-chart histogram
1个回答
1
投票

尝试一下。我使用简单的整数除法创建了结合五个X.step.的新组。接下来,我将新组的总和设为med.contamination

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

“”

reprex package(v0.3.0)在2020-03-31创建

© www.soinside.com 2019 - 2024. All rights reserved.