GGplot2:如何避免散点图的模糊输出和模糊的文本?

问题描述 投票:0回答:1

我有一个由不同年份发生的火灾组成的大型数据框,我已经成功地使用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")

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

r ggplot2 plot scatter-plot
1个回答
0
投票

基于对一个类似问题的回答 (ggplot2:模糊的面标签)你也许可以通过禁用抗锯齿来解决这个问题。包括

antialias="none" 

在你的ggsave行。

© www.soinside.com 2019 - 2024. All rights reserved.