如何摆脱水平白线出现在ggplot barplot中的地方? [英] How to get rid of horizontal white lines appear out of nowhere in ggplot barplot?
问题描述
我正在使用ggplot,并且从geom_bar中得到了那些奇怪的水平线.我无法提供一个最低限度的工作示例:同一代码很少观察到结果,并且依赖于我正在导入和转换的数据.但是,我可以显示相关的代码行,但我碰到了有人遇到了这个问题:
I'm using ggplot and I get those weird horizontal lines out of geom_bar. I cannot provide a minimal working example: the same code works with few observations and it relies on data I am importing and transforming. However, I can show the relevant line of codes and cross my fingers someone ran into this issue:
ggplot(data) + geom_bar(aes(x=Horizon, y=Importance, fill=Groups),
position='fill', stat='identity') +
theme_timeseries2() +
scale_fill_manual(values=c('#1B9E77', 'orange2', 'black',
'red2', 'blue4')) +
xlab('') + ylab('')
我的个人功能 theme_timeseries2()
并不是问题的根源:即使我在geom_bar之后停下来,它也会发生.我检查了 Importance
中的缺失值以及数据框的所有其他列,并且没有缺失的值.
My personal function, theme_timeseries2()
isn't the source of the problem: it happens even if I stop after geom_bar. I checked for missing values in Importance
and every other column of my data frame and there are none.
这也很奇怪:缩放页面上的白线与RStudio的绘图窗口中的白线不同.当我保存文件时,它们的确以.png格式打印,因此那些水平条确实存在某些问题.任何关于为什么 geom_bar()
这样做的理论都将受到高度赞赏.
It's also very odd: the white lines aren't the same on the zoomed page as in the plot window of RStudio. They do print in .png format when I save the file, so there really is something going on with those horizontal bars. Any theory about why geom_bar()
does this would be highly appreciated.
推荐答案
我猜这些线是由于进入每个小节的观测值之间的绘图错误所致.(这可能与操作系统,图形设备和/或ggplot2与它们的交互方式有关...)
I'm guessing the lines are due to a plotting bug between observations that go into each bar. (That could be related to the OS, the graphics device, and/or how ggplot2 interacts with them...)
如果您在ggplot2之前进行总结,我希望它会消失,例如:
I expect it'd go away if you summarized before ggplot2, e.g.:
library(dplyr);
data %>%
count(Horizon, Groups, wt = Importance, name = "Importance") %>%
ggplot() +
geom_col(aes(x = Horizon, y= Importance, fill = Groups), position = "fill") + ....
这篇关于如何摆脱水平白线出现在ggplot barplot中的地方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!