首页 > 解决方案 > ggplot2 PDF barplot 有白线,需要大量放大并且看起来不同。在 PowerPoint 上

问题描述

我正在尝试在多个样本的 ggplot2 中绘制堆积条形图;然而,当我绘制我的数据时,PDF 在每个条形图上都充满了白线。

这是一些示例代码,因此您可以查看我的数据的样子:

Expt <- as.data.frame(round(replicate(10, runif(10, 0, 5)), 3))
colnames(Expt) <- c("S_3", "S_5", "S_12", "S_22", "S_25", "S_27", "S_33", "S_52", "S_100", "S_101")
Expt$Phyla <- c("Proteobacteria", "Firmicutes", "Actinobacteria", "Bacteroidetes", "Verrucomicrobia", "Planctomycetes", "Cyanobacteria", "Chlamydiae", "Chloroflexi", "Deinococcus")
colors <- c("navajowhite4", "dodgerblue", "cyan", "orange", "lawngreen", "burlywood1", "black", "pink", "slateblue2", "hotpink")

library(reshape2)
Expt_melt <- melt(Expt, id.vars = "Phyla")
colnames(Expt_melt)[2:3] <- c("Sample", "Abund")

library(ggplot2)
ggplot() + geom_bar(aes(x = Sample, y = Abund, fill =     Phyla), data = Expt_melt, stat = 'identity') + scale_fill_manual(values = colors)

这段代码产生了一个很好的堆积条形图;但是,我的数据有超过 6000 行和更多的样本,而我得到的实际图像看起来与 dep 不同。在我使用的媒体上。 不同的图形

关于为什么会发生这种情况的任何想法?当我将它插入到普通的PowerPoint幻灯片中时,我需要能够使用PDF Zoomed at 150%的图表,而它看起来充满了内部白线。更不用说,最终,这些图表将被发布,我无法提交仅在 150% 放大时看起来正确的 PDF。

查看白线问题,我发现这个链接可以摆脱内部条形线: Getting Rid of Internal Bar Lines in a Bar Plot并按照他们的代码,我使用此代码绘制了我的数据:

ggplot(Expt_melt, aes(x= Sample, y=  Abund, fill = Phylum)) + stat_summary(fun.y = sum, geom = "bar") + scale_fill_manual(values = colors)

这产生了以下图表:

stat_summary 输出

正如你所看到的,这完全删除了我所有的顶栏——这是我的 Phyla (Expt_melt$Phyla),Expt_melt$Abund 值较低。更不用说,这张图必须放大 200% 才能看到“正常”。

关于做什么的任何建议?关于为什么这些图表表现出这种行为的任何想法?

谢谢!

标签: rggplot2graphicsgeom-bar

解决方案


ggplot 中条的默认宽度为 0.9(在每个级别的宽度为 1 的分类轴上),因此默认情况下,条之间存在间隙。您可能想尝试设置width = 1

ggplot() + 
    geom_bar(aes(x = Sample, y = Abund, fill =     Phyla), 
             data = Expt_melt, stat = 'identity', 
             width = 1) + 
    scale_fill_manual(values = colors) 

推荐阅读