首页 > 解决方案 > 为什么我的 geom_points 的分辨率这么差

问题描述

我在 ggplot 中发现了 Rebecca Barter 在她的博客上发布的分组条形图的替代方案,并想尝试一下。它产生一个光滑的克利夫兰点图:

在此处输入图像描述

我尝试的代码如下:

ggplot() +
  # remove axes and superfluous grids
  theme_classic() +
  theme(axis.ticks.y = element_blank(),
        text = element_text(family = "Roboto Condensed"),
        axis.text = element_text(size = rel(1.5)),
        plot.title = element_text(size = 30, color = "#000000"),
        plot.subtitle = element_text(size = 15, color = "#Ec111A"),
        plot.caption = element_text(size = 15, color = "grey25"),
        plot.margin = margin(20,20,20,20),
        panel.background = element_rect(fill = "white"),
        axis.line = element_blank(),
        axis.text.x = element_text(vjust= + 15)) +
        
   # add a dummy point for scaling purposes
  geom_point(aes(x = 15, y = P), 
             size = 0, col = "white") + 
  
  # add the horizontal discipline lines
  geom_hline(yintercept = 1:9, color = "grey80") +
  
  # add a point for each male success rate
  geom_point(aes(x = Male, y = P), 
             size = 15, col = "#00b0f0") +
  # add a point for each female success rate
  geom_point(aes(x = Female, y = P),
             size = 15, col = "#Ec111A") +
  
   geom_text(aes(x = Male, y = P, 
                label = paste0(round(Male, 1))),
            col = "black", face="bold") +
  # add the text (%) for each female success rate
  geom_text(aes(x = Female, y = P, 
                label = paste0(round(Female, 1))),
            col = "white",  face="bold") +
 #  add a label above the first two points
  geom_text(aes(x = x, y = y, label = label, col = label),
            data.frame(x = c(21.8 - 0, 24.6 - 0), y = 7.5, 
                       label = c("Male", "Female")), size = 6) +
  scale_color_manual(values = c("#Ec111A", "#00b0f0"), guide = "none") +
  
  # manually specify the x-axis
  scale_x_continuous(breaks = c(0, 10, 20, 30), 
                     labels = c("0%","10%", "20%", "30%")) +
  # manually set the spacing above and below the plot
  scale_y_discrete(expand = c(0.15, 0)) +
  labs(
    x = NULL,
    y = NULL,
    title= "Move Percentage By Gender",
    subtitle = "What Percentage Of Moves Are Tops",
    caption = "Takeaway: Males have fewer Tops and more Xs compared to Females.")

但是我的情节非常参差不齐(分辨率差),我不知道是什么原因。

在此处输入图像描述

有没有人遇到过这个问题并知道如何解决?

标签: rggplot2

解决方案


保存和分辨率取决于您的保存方式和图形设备。换句话说......你如何保存你的情节?由于这在很大程度上取决于您的个人设置和参数,因此您的里程会有所不同。ggplot2从in保存绘图的一种更可靠的方法R是使用ggsave(),您可以在其中指定这些参数并保持一定的一致性。这是一个示例绘图代码:

ggplot(mtcars, aes(disp, mpg)) +
  geom_point(size=10, color='red1') +
  geom_text(aes(label=cyl), color='white')

这将创建一个类似于您使用mtcars. 如果我直接从 R 复制并粘贴图形输出或使用导出(我正在使用 RStudio),这就是你得到的:

在此处输入图像描述

不确定你是否能分辨出来,但边缘是锯齿状的,仔细检查时看起来并不干净。对我来说绝对不行。但是,这是使用保存的相同图ggsave()

ggsave('myplot.png', width = 9, height = 6)

在此处输入图像描述

您应该能够说它更干净,因为它以更高的分辨率保存。第一个文件大小为 9 KB,而第二个文件大小为 62 KB。

最后 - 只需打开设置ggsave(),您应该会找到适合您的分辨率。如果您只是输入ggsave('myplotfile.png'),您将获得与 RStudio 中的视口窗口匹配的宽度/高度设置。您可以了解方面和大小并进行相应调整。还有一点 - 请注意文本的缩放比例与几何图形不同,因此您的圆圈的大小将与文本不同。


推荐阅读