简体   繁体   English

ggplot2 barplot:同时减小宽度和间距

[英]ggplot2 barplot: decrease width and spacing at the same time

I would like to make a barplot using ggplot. 我想使用ggplot创建一个barplot。 Ideally, my bars should be fine and there should not be a space between them. 理想情况下,我的杠应该很好,并且它们之间不应有间隔。 Any ideas how I can achieve that? 有什么想法可以实现吗?

data_hrs <- rnorm(n=513, m=20, sd=1) 
hours <- 528
days <- ceiling(hours/24)
days_hours <- days*24

data_hrs[days_hours] <- NA ## padding with NA to reach full no of days
hours <- length(data_hrs)
hours_count <- seq(1:24)
hours_count <- rep(hours_count,days)

day_count <- NA ## counts the days
for (t in 1:days){
  day_count[((t-1)*24+1):(t*24)] <- rep(t,24)
}

df_plot <- data.frame(day_count, hours_count, data_hrs)

print(ggplot(df_plot, aes(x=hours_count, y = data_hrs))+ 
      geom_bar(stat="identity", width = 0.1, position = position_dodge(width = 0.5))+
      theme_bw()+
      facet_grid(day_count ~ .)+
      scale_x_discrete(limits = c(seq(1:24)))+
      theme(axis.text.x = element_text(angle = 90, hjust = 1)))

This gives me the following (using my data): 这给了我以下内容(使用我的数据): 在此处输入图片说明

Use width = 1 to remove the space between the bars. 使用width = 1删除条之间的空间。 Create a narrow plot if you want fine bars. 如果您想要细条,请创建一个狭窄的图。 A wide graph is not compatible with fine bars without space between the bars. 宽图与细条不兼容,细条之间没有空格。

ggplot(df_plot, aes(x=hours_count, y = data_hrs))+ 
  geom_bar(stat="identity", width = 1)+
  theme_bw()+
  facet_grid(day_count ~ .)+
  scale_x_discrete(limits = c(seq(1:24)))+
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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