首页 > 解决方案 > R ggplot2 hexbin 绘图图形错误?

问题描述

我总是在我在 R 环境中使用 ggplot2 创建的每个 hexbin 密度图上发现一些奇怪的白色水平虚线,它们似乎出现在垂直相邻六边形之间的边界上。他们当然不属于那里...这种行为(错误)可以在具有不同 MS Windows 版本和不同图形驱动程序的不同计算机上的多个 R 安装上重现。我不知道如何摆脱这些虚线。下面是一个显示问题的图,以及产生该图的代码。这是一个已知问题吗?有什么提示可以克服这个吗?非常感谢您的帮助!

带有定制错误的情节

## Load libraries
library(ggplot2)
library(hexbin)

## Create some data
set.seed(222)
myx <- data.frame(x = 0.4 + rnorm(10000, mean = 0, sd = 0.1))
myy <- data.frame(y = 0.5 + rnorm(10000, mean = 0, sd = 0.1))
MyData <- data.frame(xvariable=myx$x*100, yvariable=myy$y*100)

## Prepare the plot window
dev.off(2)
windows.options(width=6, height=6)
op <- par(mfrow = c(1,1))
op <- par(oma = c(0,0,0,0) + 0.1,
          mar = c(5.1, 5.1, 4.1, 2.1))

## Creaate the plot
ggplot(MyData, aes(x=xvariable, y=yvariable) ) +
  geom_hex(bins=66) +
  xlim(0, 100) +
  ylim(0, 100) +
  ggtitle("My plot") +
  scale_fill_continuous(type = "viridis") +
  theme_bw() +
  theme(plot.title = element_text(color="black", size=17, face="bold"),
        axis.title.x = element_text(color="black", size=17, face="bold"),
        axis.title.y = element_text(color="black", size=17, face="bold"),
        axis.text=element_text(color="black", size=15, face="bold"),
        legend.title = element_text(color = "black", size = 15), 
        legend.text = element_text(color = "black", size=12),
        legend.position = c(0.9,0.2), legend.direction = "vertical")

标签: rggplot2plot

解决方案


看起来如果您保存为 pdf 然后将其转换为图像,则不会有线条。

在此处输入图像描述


推荐阅读