【发布时间】:2020-05-29 12:44:52
【问题描述】:
我有一个由不同年份发生的火灾组成的大型数据框,我已经使用 ggplot 2 成功地将这些数据绘制在散点图中,但是我在图中的所有文本周围都有一个奇怪的彩色光环。 Here is an example of the text up close.
Here is an example of what the final product looks like from far away
另外,如果我尝试将其保存为 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")
有人知道如何解决这个问题吗?
【问题讨论】:
-
您的数据示例与您的绘图代码不匹配(例如,df1 与 data_H_L 和 x = YDAY 未在 df1 中提及)。您可以编辑您的问题以使您的示例可重现吗?
-
是的,对不起,这是我的原始数据,我现在就编辑!
-
您是否尝试过将绘图输出保存为 SVG?这对我来说就像光栅图像抗锯齿伪影。
-
我不熟悉 SVG 格式,但这似乎确实有效!主要问题是我需要它以尽可能高的质量采用 Tiff、JPEG 或 PDF 格式。但你肯定让我走上了正轨谢谢!
-
PDF 也可以,因为它是矢量图形设备(例如,无质量损失的无限缩放)。 TIFF 和 JPEG 是光栅图像,因此您可能会在那里遇到一些抗锯齿伪影。
标签: r ggplot2 plot scatter-plot