首页 > 解决方案 > GGplot2:如何避免散点图输出模糊和文本模糊?

问题描述

我有一个由不同年份发生的火灾组成的大型数据框,我已经使用 ggplot 2 在散点图中成功地绘制了这些数据,但是我在图中的所有文本周围都有一个奇怪的彩色光环。 这是近距离文本的示例

这是一个从远处看最终产品的示例

此外,如果我尝试将其保存为 PDF,则只有点显示在 pdf 中,没有标签、轴或图例。

我之前在使用 geom_text() 时看到过这个问题,并且能够通过添加geom_text(..., check_overlap = TRUE). 但是,在这种情况下,我使用 element_text() 在主题中指定了所有文本,并且似乎无法找到为什么会出现这种奇怪颜色的答案。

这是我的数据示例:

df1 <- data.frame(REP_DATE = c("1988-05-02", "2015-04-18", "1981-06-19", "2009-06-18"),
                      YEAR = c("1988", "2015", "1981", "2009"),
                      CAUSE = c("L", "H", "L", "H"),
                      CALC_HA=c("2350.18324","2350.18324", "1825.316641", "2296.424534"))

以及我创建情节的原始代码:

#load your packages
library(ggplot2)
library(RColorBrewer)
library(dplyr)
library(anytime)
library(PCICt)
library(lubridate)
library(forcats)
library(ggExtra)

# first we need to make it a PCICt object
#set Rep_date as a date
data$REP_DATE <- as.Date(data$REP_DATE, "%Y-%m-%d")
data$YDAY <- anydate(yday(data$REP_DATE))
class(data$YDAY)

#subset just the data with H and L
data_H_L <- data[data$CAUSE=="H" | data$CAUSE=="L",]

p<-ggplot(data_H_L, aes(x = YDAY,                                            #set x and Y Axis
                        y = YEAR, colour=CAUSE, size=CALC_HA)) +             #set the colour and size 
  scale_y_reverse()+                        # reverse Y scale to get the years at the top to bottom order
  guides(size=FALSE)+                                                        #remove size legend 
  geom_point(alpha=0.2) +                                                    #set the transparency 
  #scale_color_brewer(type="qual", palette="Dark2")+                         #change the colours here, Dark2 looks good
  scale_colour_manual(values=c("#0195D6", "#E66407"),
                      name  ="Fire Cause",
                      breaks=c("H", "L"),
                      labels=c("Human", "Lightning"))+                       #specify colours using unique colour combo
  scale_x_date(date_breaks = "1 month",date_labels="%b")+  
  #scale_colour_discrete(values=c("#F93800", "#FFB500"),
  removeGrid(x=TRUE, y=FALSE)+                                               # remove x grid
  theme(plot.background = element_rect(fill = "black"),                      #set plot background colour
        panel.background=element_rect(fill="black"))+                        #set panel colour
  scale_size_continuous(range=c(1.75,10))+                                   #set scale for size of circles                                     
  guides(colour = guide_legend(override.aes = list(size=4, alpha=1)))        #set size of circles in legend

pp<-p+ theme(axis.text.x = element_text(face="bold", colour="white", size=10, family="sans"),  #set X axis text
             axis.text.y = element_text(face="bold", colour="white", size=10, family="sans"), #set y axis text
             axis.ticks = element_blank(),
             panel.grid.major.y = element_line(linetype="solid",size=1),#set y axis line width
             plot.title = element_text(color="white", size=14, face="bold", hjust=0.5, family="sans"),#set main title
             plot.subtitle = element_text(color="white", size=12, face="bold", hjust=0.5,family="sans"),#set subtitle 
             axis.title.x = element_blank(),                                       #set x axis title details
             axis.title.y = element_blank(),                                       #set y axis title details
             legend.text = element_text(colour="white", size=10, face="bold",family="sans"), #set legend axis title details
             legend.background = element_rect(fill="black", size=1, linetype="solid", colour= "white"), #set legend background
             legend.title = element_text(colour="white", size=12, face="bold",family="sans"),
             legend.title.align = 0.5,                                      #set legend title
             legend.key=element_blank())                              #set legend key to blank to get rid of background around dot
ggsave("test2.jpg", units="in", width=10, height=7, dpi=300)
ggsave(pp, filename = "Full_Point_Density.tiff", dpi = 900, type = "cairo",width = 10, height = 7, units = "in")

有谁知道如何解决这个问题?

标签: rggplot2plotscatter-plot

解决方案


根据对类似问题(ggplot2: blurry facet labels)的回答,您可以通过禁用抗锯齿来解决此问题。包括

antialias="none" 

在您的 ggsave 行中。


推荐阅读