在ggplot中更改条形之间的空间

时间:2017-06-30 10:45:00

标签: r ggplot2

我想在我的ggplot中的栏之间设置适当的空格。

它的样子:

enter image description here

该数据:

> dput(tbl_all_ribosomes_abundance_overlap_dat)
structure(list(Ribosome = c("Plastid", "Plastid", "Cytosol", 
"Cytosol", "Plastid", "Plastid", "Cytosol", "Cytosol", "Plastid", 
"Plastid", "Cytosol", "Cytosol", "Plastid", "Plastid", "Cytosol", 
"Cytosol", "Plastid", "Plastid", "Cytosol", "Cytosol", "Plastid", 
"Plastid", "Cytosol", "Cytosol", "Plastid", "Plastid", "Cytosol", 
"Cytosol", "Plastid", "Plastid", "Cytosol", "Cytosol", "Plastid", 
"Plastid", "Cytosol", "Cytosol", "Plastid", "Plastid", "Cytosol", 
"Cytosol", "Plastid", "Plastid", "Cytosol", "Cytosol", "Plastid", 
"Plastid", "Cytosol", "Cytosol", "Plastid", "Plastid", "Cytosol", 
"Cytosol", "Plastid", "Plastid", "Cytosol", "Cytosol", "Plastid", 
"Plastid", "Cytosol", "Cytosol", "Plastid", "Plastid", "Cytosol", 
"Cytosol", "Plastid", "Plastid", "Cytosol", "Cytosol", "Plastid", 
"Plastid", "Cytosol", "Cytosol", "Plastid", "Plastid", "Cytosol", 
"Cytosol", "Plastid", "Plastid", "Cytosol", "Cytosol"), Size_ranges = c(10L, 
10L, 10L, 10L, 34L, 34L, 34L, 34L, 59L, 59L, 59L, 59L, 84L, 84L, 
84L, 84L, 110L, 110L, 110L, 110L, 134L, 134L, 134L, 134L, 165L, 
165L, 165L, 165L, 199L, 199L, 199L, 199L, 234L, 234L, 234L, 234L, 
257L, 257L, 257L, 257L, 362L, 362L, 362L, 362L, 433L, 433L, 433L, 
433L, 506L, 506L, 506L, 506L, 581L, 581L, 581L, 581L, 652L, 652L, 
652L, 652L, 733L, 733L, 733L, 733L, 818L, 818L, 818L, 818L, 896L, 
896L, 896L, 896L, 972L, 972L, 972L, 972L, 1039L, 1039L, 1039L, 
1039L), Abundance = c(15236000, 36722900, 8953800, 8975200, 45971100, 
99067800, 20305100, 26360500, 91416000, 219396000, 71722610, 
84034100, 230630600, 254380400, 52402800, 50422300, 354388700, 
437609200, 72343800, 86587600, 462991400, 497156100, 51817210, 
65168400, 235045400, 290932300, 41362400, 50220300, 168440600, 
196797700, 107294900, 158465800, 362305500, 375372400, 158236400, 
209906300, 1367158670, 1122186600, 203436000, 203426900, 978435120, 
983735500, 373804400, 362937700, 2104276960, 2220253960, 394261700, 
410656800, 1267607300, 1323143500, 285954000, 276834600, 1080187900, 
1410693500, 1261120700, 1299253600, 840738200, 1226346700, 1591379300, 
1829220400, 1139340100, 1758611700, 2003838200, 2346221200, 2425368000, 
3150723500, 2658315400, 3184999800, 3141234200, 4124976700, 3484620300, 
4111995200, 2880830100, 4403308600, 4233078100, 4960194500, 2923459800, 
4360192600, 5238580300, 5494148500), Time = structure(c(1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("EOD", 
"EON"), class = "factor")), .Names = c("Ribosome", "Size_ranges", 
"Abundance", "Time"), row.names = c(NA, -80L), class = "data.frame")

这是我用于绘图的代码:

ggplot(data=tbl_all_ribosomes_abundance_overlap_dat, mapping=aes(x=Size_ranges, y=Abundance, fill=Time)) +
  geom_bar(stat="identity", position="dodge", width = 25) +
  scale_x_continuous(limits = c(0, 1000), 
                     breaks = c(0, 100, 250, 500, 750, 1000)) +
  scale_fill_manual("legend", values = c("EOD" = "gold1", "EON" = "gray")) +
  facet_grid(Ribosome~.)

正如您在图表左侧看到的那样,它确实看起来很混乱,因为条形之间的空间不存在。另一方面,右侧栏之间的空间太大。您是否知道如何通过增加和减少特定位置的空间来改进此图形?

关于为y轴设置不同比例以使其更清晰(左侧)的建议非常受欢迎。

1 个答案:

答案 0 :(得分:4)

您可以使x变量离散而不是连续。您将值视为分类而不是数字。这将为每个值创建一个均匀间隔的条。

library(dplyr)
tbl_all_ribosomes_abundance_overlap_dat %>% 
  mutate(Size_ranges = factor(Size_ranges)) %>% 
  ggplot(aes(x=Size_ranges, y=Abundance, fill=Time)) +
  geom_bar(stat="identity", position = "dodge") +
  scale_fill_manual("legend", values = c("EOD" = "gold1", "EON" = "gray")) +
  facet_grid(Ribosome~.) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

enter image description here