r - 背景和文本在 ggplot 散点图的 PDF 输出中没有正确显示?
问题描述
我有一个包含不同年份发生的火灾的大型数据框,我已经使用 ggplot 2 成功地将这些数据绘制在散点图中。问题是我的文本或主题()规范都没有出现在 pdf 中。R 图看起来是正确的,但是,在导出 PDF 时只包含点。
这是我的数据示例:
# variable name df1 changed to name data
data <- 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(pp, filename = "whatever.pdf", device = cairo_pdf, width = 4, height = 3, units = "in") #p changed to pp
我已经尝试了以下建议,但无法弄清楚为什么文本不会显示,指定的黑色背景也不会显示。
解决方案
推荐阅读
- doctrine-orm - Doctrine2 - 如何为表单映射定义表前缀
- python - python pandas pivot_table列一级错误名称
- c - 定义的宏 '__CCP_H__' 保留给编译器 [MISRA 2012 Rule 21.1,必需]
- json - 从 JSON 获取正确的集合格式
- sql - SQL concat 值作为列
- c# - Nancy Modules 是全球发现的,如何用 .net 实现相同的想法?
- performance - 即使使用索引,sqlite 的查询速度也很慢
- php - 在php中文件下载期间使用flush和ob_flush的正确方法
- react-native - 在 detox 中绕过 webviews
- python - Python OOP - 方法返回无