我正在尝试创建一个框架来轻松绘制我们的数据集。当前的想法是启动一个ggplot
图形,向其中添加图层,然后显示或保存它。我的代码如下所示:
initPlot <- function(title = "", data = NULL){
if(is.null(data)) data <- GLOBDATA
plot <- ggplot(data, aes(jahr))
plot <- plot + scale_x_continuous(breaks = seq(2001, 2012, 1))
textTheme <- element_text(size=6, face="plain", color="black", family="AvantGarde")
lineTheme <- element_line(color="black", size=0)
plot <- plot + theme(
text = textTheme,
axis.text = textTheme,
axis.ticks = lineTheme,
axis.line = lineTheme,
axis.title = element_blank(),
plot.background = element_rect(fill="#f0f0f0"),
strip.background = element_rect(fill="#f0f0f0"),
panel.background = element_rect(fill="#f0f0f0"),
panel.grid = element_blank(),
legend.position = "bottom"
)
plot <- plot + guides(color = guide_legend(title = title))
PLOTGLOB <<- plot
plot
}
plotConfidence <- function(columns, color = "red", title = "", label = "", plot = NULL){
plot <- plotLine(columns, "black", label, plot, 1)
plot <- plot + geom_ribbon(columns, alpha = 0.3, fill = color, linetype=0)
PLOTGLOB <<- plot
plot
}
plotLine <- function(column, color = "black", label = "", plot = NULL, size = 1){
if(is.null(plot)) plot <- PLOTGLOB
plot <- plot + geom_line(column, color = color, size = size)
PLOTGLOB <<- plot
plot
}
然后我这样调用我的代码:
initPlot("title")
plotConfidence(
aes(
y = jSOEP_aqne_ip_fgt060_f_alle,
ymin = jSOEP_aqne_ip_lfgt060_f_alle,
ymax = jSOEP_aqne_ip_ufgt060_f_alle, color="Alle", fill="Alle"
),
"red")
plotConfidence(
aes(
y = jSOEP_aqne_ip_fgt060_f_mann,
ymin = jSOEP_aqne_ip_lfgt060_f_mann,
ymax = jSOEP_aqne_ip_ufgt060_f_mann, color="Männer", fill="Männer"
),
"blue", , label="Männer")
这会产生以下图形:
如您所见,图例颜色与相应的 geom_ribbons 不匹配,实际上,两者都是“蓝色”颜色(通过暂时将 alpha 设置为 1 发现)。我该如何解决?
这是我要绘制的数据:
GLOBDATA <- structure(list(jSOEP_aqne_ip_fgt060_f_alle = c(0.117169998586178,
0.122670002281666, 0.131659999489784, 0.132029995322227, 0.140119999647141,
0.142869994044304, 0.136739999055862, 0.140990003943443, 0.146730005741119,
0.149069994688034, 0.141920000314713, 0.142879992723465), jSOEP_aqne_ip_lfgt060_f_alle = c(0.114249996840954,
0.119199998676777, 0.128110006451607, 0.12814000248909, 0.136230006814003,
0.139119997620583, 0.132400006055832, 0.137409999966621, 0.142560005187988,
0.14478999376297, 0.137840002775192, 0.138579994440079), jSOEP_aqne_ip_ufgt060_f_alle = c(0.120090000331402,
0.126139998435974, 0.135220006108284, 0.135920003056526, 0.143999993801117,
0.146630004048347, 0.141090005636215, 0.144580006599426, 0.15090000629425,
0.153359994292259, 0.146009996533394, 0.147180005908012), jSOEP_aqne_ip_fgt060_f_mann = c(0.100199997425079,
0.106820002198219, 0.117770001292229, 0.117349997162819, 0.126489996910095,
0.130469992756844, 0.12601999938488, 0.127340003848076, 0.132960006594658,
0.135379999876022, 0.132510006427765, 0.13782000541687), jSOEP_aqne_ip_lfgt060_f_mann = c(0.0951400026679039,
0.101929999887943, 0.112829998135567, 0.112510003149509, 0.121720001101494,
0.12372999638319, 0.120829999446869, 0.121650002896786, 0.127389997243881,
0.128470003604889, 0.12533999979496, 0.131980001926422), jSOEP_aqne_ip_ufgt060_f_mann = c(0.105259999632835,
0.111709997057915, 0.122720003128052, 0.122189998626709, 0.131270006299019,
0.137209996581078, 0.131219998002052, 0.133019998669624, 0.138539999723434,
0.142289996147156, 0.139679998159409, 0.143659994006157)), .Names = c("jSOEP_aqne_ip_fgt060_f_alle",
"jSOEP_aqne_ip_lfgt060_f_alle", "jSOEP_aqne_ip_ufgt060_f_alle",
"jSOEP_aqne_ip_fgt060_f_mann", "jSOEP_aqne_ip_lfgt060_f_mann",
"jSOEP_aqne_ip_ufgt060_f_mann"))