当与ggplot函数geom_bar一起使用连续刻度时,有什么方法可以避免重叠的条形?

时间:2019-06-26 15:28:55

标签: r ggplot2 geom-bar

我正在处理一些数据,而我想做的事情如下图所示。条形遵循连续的比例(时间段),bar_width大(易于阅读),没有重叠的问题。 Model

使用我的数据集和Rstudio,我只能得到以下内容:

MyResult

您看到,有很多条重叠(重叠的标签对我来说不是问题,只是实际的条)。当我从Rstudio中保存参数时,我尝试调整bar_width之类的参数,以及宽度和高度,但总是有问题。当没有重叠时,它太小而条太细等。

在这里您可以找到我的脚本以及获取图形所需的数据:MyData

我希望我能遵循stackoverflow的所有说明,请原谅我英文不好,最感谢您的帮助!

编辑:我的数据样本(但是要重现错误,您将需要全部内容)

   color_by label_by Sample    Abundance
1        Ar      Chl      2 0.0050024660
2        Ar      Rho      2 0.0014796026
3        Ar      Str      2 0.0031001198
4        Ar  Unknown      2 0.0000000000
5        Al      Api      2 0.0076798422
6        Al      Cil      2 0.0073980131
7        Al      Din      2 0.0228281547
8        Al  Unknown      2 0.0002113718
9        Op      Cho      2 0.0000000000
10       Op      Fun      2 0.0007045727

这是我用来获取“ MyResult”图的代码示例:

counts_long<-read.table("data.csv", header=TRUE,sep= ",")
counts_long$X<-NULL
clr_pal<-c("#B8143C", "#E6194B" ,"#EE6587", "#F6B2C3", "#30903C" ,"#3CB44B", "#7DCD87" ,"#BEE6C3",
        "#CCB414" ,"#FFE119", "#FFE74C" ,"#FFEE7F", "#FFF5B2" ,"#4363d8", "#8197E5", "#f58231",
        "#F8AB75", "#911eb4", "#B569CD", "#DAB4E6", "#42d4f4", "#81E2F7", "#C028B8", "#F032E6",
        "#F576EE", "#FABAF6", "#bfef45", "#000000")
bar_width=3

p <- ggplot2::ggplot(counts_long, aes(x = Sample, y = Abundance, fill = label_by)) +
ggplot2::geom_bar(position = "stack", stat = "identity", width = bar_width) +
ggplot2::guides(fill=guide_legend(title = "legend", ncol = 1)) +
ggplot2::scale_fill_manual(values = clr_pal) +
xlab("Depth(cm)")+
scale_x_reverse(breaks=counts_long$Sample)+    
ggplot2::scale_y_continuous(expand = c(0,0)) +
ggplot2::theme(axis.line.x = element_line(colour = 'grey'),
             axis.line.y = element_line(colour = 'grey'),
             axis.ticks = element_line(colour = 'grey'),
             axis.text.x = element_text(angle = 90, family = "Helvetica",
                                        size = 6, hjust = 1, vjust = 0.5),
             legend.background = element_rect(fill = 'transparent', colour = NA),
             legend.key = element_rect(fill = "transparent"),
             legend.key.size = unit(0.4, "cm"),
             panel.background = element_rect(fill = 'transparent', colour = NA),
             panel.grid.major.x = element_blank(),
             panel.grid.major.y = element_line(colour = adjustcolor('grey', 0.2)),
             panel.grid.minor = element_line(colour = NA),
             plot.background = element_rect(fill = 'transparent', colour = NA),
             plot.title = element_text(hjust = 0.5),
             strip.background = element_blank(),
             strip.text = element_text(family = "Helvetica", size = 8, face = "bold"),
             text = element_text(family = "Helvetica", size = 8))


p+coord_flip()

1 个答案:

答案 0 :(得分:0)

您可以尝试两种方法:

  • 在rstudio中单击绘图上方的缩放按钮,然后看是否更好。您也应该能够以这种方式导出它。
  • 否则在Rmd中绘制图并在R块选项中设置图形的大小(例如r fig.width = 10,fig.height = 10)