I've three replicates of my experimental sample(F) and three replicates of control samples(C). Each experimental sample has 100 data points whereas each control has 70 data points. For the experimental data points, there are sub-categories like 4E,5E,7E,8E and for control there is only a single category CE.
Here's the code to generate some emulated data:
library(ggplot2)
set.seed(12345)
evals <- c( rep("4E",20), rep("5E",20), rep("7E",40), rep("8E",20))
subE <- c(sample(evals),sample(evals),sample(evals),rep("CE",70),rep("CE",70),rep("CE",70))
pwvr <- c(rnorm(100),rnorm(100),rnorm(100),rnorm(70,1.0),rnorm(70,1.1),rnorm(70,1.2))
expT <- c(rep("F",100*3),rep("C",70*3))
repX <- c(rep(1,100),rep(2,100),rep(3,100),rep(1,70),rep(2,70),rep(3,70))
myData.df <- data.frame(subE=as.factor(subE), pwvr = pwvr, expT= as.factor(expT), repX= as.factor(repX))
dim(myData.df)
What I want to do, is to draw Box and Jitter plots for the factor levels 4E,5E,7E,8E along with a boxplot of the combined values of these four levels. I don't know how to do that. Do I need to create another level clubbing all the corresponding values?
Secondly can anybody tell how to reorganize the appearance of orders in along the X-axis, like how to plot the following order 8E,7E,5E,4E etc.
The following code generates the plot for individual levels, but I need to have the box/jitter for the combined levels as well.
myGreen <- "forestgreen"
myBlue <- "dodgerblue2"
allboxCol <- c(rep(myGreen,1),rep(myBlue,1))
pw.boxplot <- ggplot(myData.df, aes(x=subE,y=pwvr, fill= expT)) +
geom_jitter(position=position_jitter(width=.2, height=0),alpha=0.15, aes(col= expT)) + scale_color_manual(values=allboxCol) +
geom_boxplot(outlier.shape = NA, fatten = 0.01, lwd=1.0,alpha=0.5,width=0.6) +
theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))
pw.boxplot + scale_fill_manual(values=allboxCol) + facet_grid( .~ expT + repX , scales="free", space = "free" )
do you mean?
myGreen <- "forestgreen"
myBlue <- "dodgerblue2"
allboxCol <- c(rep(myGreen,1),rep(myBlue,1))
allboxCol <- c(alpha(allboxCol, 0.2),allboxCol[2])
library(tidyverse)
myData.df %>%
as_tibble() %>%
mutate(subE = paste0("total_", expT, repX)) %>%
filter(expT != "C") %>%
bind_rows(myData.df) %>%
mutate(fill = ifelse(grepl("total", subE), paste0("total_",expT), expT)) %>%
ggplot(aes(x=subE,y=pwvr, fill= fill)) +
geom_jitter(position=position_jitter(width=.2, height=0),alpha=0.15, aes(col= fill), show.legend = F) +
geom_boxplot(outlier.shape = NA, fatten = 0.01, lwd=1.0,width=0.6, show.legend = F) +
scale_fill_manual(values=allboxCol) +
scale_color_manual(values=allboxCol) +
theme_bw() +
theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
facet_grid( .~ expT + repX , scales="free", space = "free" )
It is indeed an easy solution to just repeat the data but assigning a new level, I called it all. You can explicitly specify the order of the levels:
library(ggplot2)
library(dplyr)
set.seed(12345)
evals <- c( rep("4E",20), rep("5E",20), rep("7E",40), rep("8E",20))
subE <- c(sample(evals),sample(evals),sample(evals),rep("CE",70),rep("CE",70),rep("CE",70))
pwvr <- c(rnorm(100),rnorm(100),rnorm(100),rnorm(70,1.0),rnorm(70,1.1),rnorm(70,1.2))
expT <- c(rep("F",100*3),rep("C",70*3))
repX <- c(rep(1,100),rep(2,100),rep(3,100),rep(1,70),rep(2,70),rep(3,70))
myData.df <- data.frame(subE=subE, pwvr = pwvr, expT= expT, repX= repX,
stringsAsFactors = FALSE)
add_data <- myData.df %>%
filter(subE != "CE") %>%
mutate(subE = "all")
myData.df <- bind_rows(myData.df, add_data)
myData.df <- myData.df %>%
mutate(subE = as.factor(subE),
subE = factor(subE, levels = levels(subE)[c(4, 3, 2, 1, 5, 6)]))
myGreen <- "forestgreen"
myBlue <- "dodgerblue2"
allboxCol <- c(rep(myGreen,1),rep(myBlue,1))
pw.boxplot <- ggplot(myData.df, aes(x=subE,y=pwvr, fill= expT)) +
geom_jitter(position=position_jitter(width=.2, height=0),alpha=0.15, aes(col= expT)) + scale_color_manual(values=allboxCol) +
geom_boxplot(outlier.shape = NA, fatten = 0.01, lwd=1.0,alpha=0.5,width=0.6) +
theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))
pw.boxplot + scale_fill_manual(values=allboxCol) + facet_grid( .~ expT + repX , scales="free", space = "free" )
Related
I am making a plot for likeability of different ice cream flavours by school. I already have the code for making the plot, but I'm stuck on calculating and adding the error bars to the plot. I know summarise and geom_errorbar works, but I'm not sure how to calculate standard error with a long data format.
Here's reproducible code:
IDs <- seq(1,50)
IDs <- data.frame(rep(IDs, each = 5))
names(IDs)[1] <- "ID"
tastes <- c("Strawberry", "Vanilla", "Chocolate", "Matcha", "Sesame")
tastes <- data.frame(rep(tastes, times = 50))
#random numbers for schools
A <- runif(250, 1,5)
B <- runif(250, 1,5)
C <- runif(250, 1,5)
#merge
test <- cbind(IDs, tastes)
test <- cbind(test, A)
test <- cbind(test, B)
test <- cbind(test, C)
names(test)[2] <- "Flavour"
#make long
test_long <- melt(test,
id.vars = c("ID", "Flavour"))
#plot
plot <- ggplot(test_long) +
geom_bar(aes(x = Flavour,
y = value), stat="summary", fun=mean) +
scale_x_discrete(labels=c("C","M","S","S","V")) +
coord_cartesian(ylim=c(1,5)) +
facet_grid(. ~ variable) +
labs(title = "Likeability of Different Flavours by School") +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))
plot
Any ideas on how to calculate and add error bars for each bar in the plot? Thank you!
One possible solution could be using a new data.frame generated with summarise
library(dplyr)
summary_test <-
test_long %>%
group_by(Flavour, variable) %>%
summarise(mean = mean(value),
SE = sd(value) / sqrt(n()))
#plot
plot <- ggplot(summary_test, aes(x = Flavour, y = mean)) +
geom_errorbar(aes(ymin = mean - SE, ymax = mean + SE)) +
geom_bar(stat = "identity") +
scale_x_discrete(labels=c("C","M","S","S","V")) +
coord_cartesian(ylim=c(1,5)) +
facet_grid(. ~ variable) +
labs(title = "Likeability of Different Flavours by School") +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))
plot
Hello everyone I was trying to add some text below the x axis in ggplot2 and I was able to do so using geom_textand with help of coord_cartesian but I couldn't make it reproducible as this need to run in a loop. I thought that adding the values I want with the row names (First, Second) in a table would fix it, does anybody have experience in that. below is the workaround I did. Thank you very much in advance.
## Data
Grade <- 1 : 20
Case <- rep(paste('case' , 1:5,sep = ''),4)
Number <- paste('n', 1:20 , sep = '')
Class <- c(rep('Class1',5) , rep('Class2',5) , rep('Class3',5) , rep('Class4',5))
se <- 0.2
df <- data.frame(Grade,Case ,Number, Class , se)
## plot
ggplot(df, aes(x= factor(Case , levels = c('case1','case2' , 'case3' , 'case4','case5')) , y=Grade ,
fill= Grade)) +
geom_bar(position="dodge", stat="identity",
colour="black",
size=.4) +
geom_errorbar(aes(ymin=Grade +se, ymax=Grade +se),
size=.3,
width=.2,
position=position_dodge(.9))+
geom_linerange(aes(ymin = Grade , ymax = Grade +se),position=position_dodge(.9))+
geom_text(aes(label=Number , y = Grade + se + 1),data=df, position=position_dodge(0.9), size= 4) +
ggtitle('Place a table below x axis')+
facet_grid(~Class) +
xlab('') +
ylab('Case Num') +
theme_gray()+
theme(plot.margin = unit(c(1,1,1,6), "lines"),
axis.text.x = element_text(size = 15)) +
scale_x_discrete(labels = paste(1:5 , '\n' , 10:15, sep = '')) +
geom_text(data = df[df$Class == 'Class1',],x = -1 , y = -3,
label= 'First\nSecond' , size = 4)+
coord_cartesian(clip = "off" , xlim = c(1, 5) )
EDIT:
Sorry for the confusion,although the solution suggested by #stefan is pretty much convenient but the main purpose is to have something like this:
considering that the proposed table will contain external characters, not taken from the data frame at all (if possible!).
As an alternative approach to tackle this problem I simply set up the table as a second ggplot which I glue together with the major ggplot using patchwork.
## Data
Grade <- 1 : 20
Case <- rep(paste('case' , 1:5,sep = ''),4)
Number <- paste('n', 1:20 , sep = '')
Class <- c(rep('Class1',5) , rep('Class2',5) , rep('Class3',5) , rep('Class4',5))
se <- 0.2
df <- data.frame(Grade,Case ,Number, Class , se)
library(patchwork)
library(ggplot2)
library(tidyr)
library(dplyr)
## plot
p1 <- ggplot(df, aes(x= factor(Case , levels = c('case1','case2' , 'case3' , 'case4','case5')) , y=Grade ,
fill= Grade)) +
geom_bar(position="dodge", stat="identity",
colour="black",
size=.4) +
geom_errorbar(aes(ymin=Grade +se, ymax=Grade +se),
size=.3,
width=.2,
position=position_dodge(.9))+
geom_linerange(aes(ymin = Grade , ymax = Grade +se),position=position_dodge(.9))+
geom_text(aes(label=Number , y = Grade + se + 1),data=df, position=position_dodge(0.9), size= 4) +
ggtitle('Place a table below x axis')+
facet_grid(~Class) +
xlab(NULL) +
ylab('Case Num') +
theme_gray()+
theme(axis.text.x = element_blank())
p2 <- df %>%
mutate(First = as.integer(stringr::str_extract(Case, "\\d")),
Second = First + 9,
Third = Second + 9) %>%
pivot_longer(c(First, Second, Third), names_to = "layer", values_to = "label") %>%
ggplot(aes(x = Case)) +
geom_text(aes(y = factor(layer, c("Third", "Second", "First")), label = label)) +
labs(y = "", x = NULL) +
theme_minimal() +
theme(axis.line = element_blank(), axis.ticks = element_blank(), axis.text.x = element_blank(),
panel.grid = element_blank(), strip.text = element_blank()) +
facet_grid(~Class)
p1 / p2 + plot_layout(heights = c(8, 1))
Created on 2020-05-23 by the reprex package (v0.3.0)
EDIT: Tweak to get a more table like output by adding a geom_tile and removing the spacing between facets as well as setting expansion of x-axis to zero:
p2 <- df %>%
select(Case, Class) %>%
mutate(First = letters[1:nrow(.)],
Second = LETTERS[1:nrow(.)],
Third = as.character(1:nrow(.))) %>%
pivot_longer(c(First, Second, Third), names_to = "layer", values_to = "label") %>%
ggplot(aes(x = Case, y = factor(layer, c("Third", "Second", "First")))) +
# Add Table Style
geom_tile(fill = "blue", alpha = .4, color = "black") +
geom_text(aes(label = label)) +
# Remove expansion of axsis
scale_x_discrete(expand = expansion(mult = c(0, 0))) +
labs(y = "", x = NULL) +
theme_minimal() +
theme(axis.line = element_blank(), axis.ticks = element_blank(), axis.text.x = element_blank(),
panel.grid = element_blank(), strip.text = element_blank(), panel.spacing.x = unit(0, "mm")) +
facet_grid(~Class)
p1 / p2 + plot_layout(heights = c(8, 1))
Created on 2020-05-24 by the reprex package (v0.3.0)
If I understand your requirement correctly, (as in my comment above), this may help you. You just need to name your graph and add the labels in loop and render outside the loop.
...
theme(plot.margin = unit(c(1,1,1,6), "lines"),
axis.text.x = element_text(size = 15)) +
scale_x_discrete(labels = paste(1:5 , '\n' , 10:15, sep = '')) +
coord_cartesian(clip = "off" , xlim = c(1, 5) )
label = NULL
ordinal <- c('first','second','third','fourth','fifth','sixth','seventh','eighth','ninth','tenth')
for (i in 1:5) {
label <- paste(label, '\n', ordinal[i])
}
g1 <- g1 + geom_text(data = df[df$Class == 'Class1',],x = -1 , y = -3,
label= label , size = 4)
g1
This is what I get as a result:
I have the following code. How can I make the grid much finer scale? Also not sure why the legend is not showing? Like I want to have 4 o 8 squares between 75 and 100.
#Select the colors from http://colorbrewer2.org/#type=sequential&scheme=RdPu&n=9
if( !is.element("ggplot2", installed.packages()[,1]) )
install.packages("ggplot2")
if( !is.element("grid", installed.packages()[,1]) )
install.packages("grid")
if( !is.element("plyr", installed.packages()[,1]) )
install.packages("plyr")
if( !is.element("data.table", installed.packages()[,1]) )
install.packages("data.table")
if( !is.element("igraph", installed.packages()[,1]) )
install.packages("igraph")
if( !is.element("ggthemes", installed.packages()[,1]) )
install.packages("ggthemes")
library(ggplot2)
library(grid)
library(plyr)
library(data.table)
library(igraph)
library(ggthemes)
#setwd(dir = "/home/sathya/Documents/coreset/rplots/fig/svm")
svmdata <- fread("ionosphere-smooth-ls-optim.dat")
#svmdata <- read.table("ionosphere-smooth-ls-optim.dat", header=TRUE)
svmdata <- rename(svmdata, c("# k"="k"))
svmdata <- head(svmdata, 100)
svmdata_ls <- fread("ionosphere-ls-optim.dat")
svmdata_ls <- head(svmdata_ls,100)
svmdata_ls <- rename(svmdata_ls, c("# k"="k"))
y_max <- max(max(svmdata$gap), max(svmdata_ls$gap))
base <- ggplot(data=svmdata_ls,aes(x=k, y=gap, group=2)) +
geom_line(colour="#dd3497", size=1.5) + #geom_point(size=4, shape=21) +
geom_line(data=svmdata,aes(x=k, y=gap, group=1 ), colour="#54278f", size=1.5) +
#ggtitle("svmdata_ls gap and svmdata gap vs k") +
geom_smooth(alpha=.2, size=1) +
geom_abline(colour = "grey50", size = 2) +
xlim(0, max(svmdata$k)) +
ylim(0, y_max) +
scale_colour_manual("",
breaks = c("svmdata_ls", "svmdata"),
values = c("#dd3497", "#54278f"))
labelled <- base +
labs(
x = "K",
y = "Gap",
colour = "Cylinders",
title = "svmdata_ls gap and svmdata gap vs k"
)
labelled
styled <- labelled +
theme_bw() +
theme(
plot.title = element_text(face = "bold", size = 25, colour = "purple"),
axis.title=element_text(size= 20, face= "bold", colour="blue"),
legend.background = element_rect(fill = "white", size = 4, colour = "white"),
legend.justification = c(0, 1),
legend.key = element_rect(fill = "yellow"),
#legend.position = "bottom",
legend.position = c(0, 1),
axis.ticks = element_line(colour = "grey70", size = 0.25),
panel.grid.major = element_line(colour = "grey70", size = 0.5),
panel.grid.minor = element_line(colour= "grey70", size=0.5)
)
styled
Data: http://pastebin.com/0wb6S4m8 and http://pastebin.com/L4sdEyYZ
These are just the basics; I didn't bother with all the special colours etc..
to get a legend, combine your data into one long data frame and use aes(colour=grp), where grp is a variable that identifies the group.
to get finer-spaced tick marks, use scale_x_continuous() with breaks or minor_breaks set.
Get data:
rr <- function(x) plyr::rename(as.data.frame(data.table::fread(x)),
c("# k"="k"))
svmdata <- rr("ionosphere-smooth-ls-optim.dat")
svmdata_ls <- rr("ionosphere-ls-optim.dat")
Combine data into a single labeled data frame:
get_vars <- function(d) d[c("k","gap")]
svmcomb <- plyr::ldply(list(svmdata=svmdata,svmdata_ls=svmdata_ls),
get_vars)
Plot:
library(ggplot2)
ggplot(svmcomb,aes(k,gap,colour=.id))+
geom_line()+
scale_x_continuous(minor_breaks=seq(0,100,by=2.5))
Typically with ggplot you would approach this type of thing more like this:
svmdata$grp <- "svmdata"
svmdata_ls$grp <- "svmdata_ls"
dat <- rbind(svmdata,svmdata_ls)
base <- ggplot(data=dat,aes(x=k, y=gap, colour = grp,group=grp)) +
geom_line(size=1.5) + #geom_point(size=4, shape=21) +
geom_line(size=1.5) +
#ggtitle("svmdata_ls gap and svmdata gap vs k") +
geom_smooth(alpha=.2, size=1) +
geom_abline(colour = "grey50", size = 2) +
xlim(0, max(svmdata$k)) +
ylim(0, y_max) +
scale_colour_manual("",
breaks = c("svmdata_ls", "svmdata"),
values = c("#dd3497", "#54278f"))
I have a 3 rows by 5 columns facet plot. Each row show data which spread over different ranges. To properly display my data so everything is shown, I don't set a y axis limit.
Here's my code:
require(reshape2)
library(ggplot2)
library(RColorBrewer)
fileName = paste("./data_test.csv", sep = "")
## data available here: https://dl.dropboxusercontent.com/u/73950/data_test.csv
mydata = read.csv(fileName,sep=",", header=TRUE)
dataM = melt(mydata,c("id"))
dataM = cbind(dataM,
colsplit(dataM$variable,
pattern = "_",
names = c("Network_model", "order", "category")))
dataM$variable <- NULL
dataM <- dcast(dataM, ... ~ category, value.var = "value")
dataM$minCut <- NULL
dataM$nbr_communities <- NULL
dataM$mean_community_size <- NULL
dataM$density <- NULL
my_palette <- colorRampPalette(rev(brewer.pal(11, "Spectral")))
dataM = melt(dataM, id.vars = c("Network_model", "order", "nodesRemoved", "id"))
my_palette = c(brewer.pal(5, "Blues")[c(4)], brewer.pal(5, "Set1")[c(3)])
ggplot(dataM, aes(x= nodesRemoved ,y= value, group= order, color= order)) +
geom_point(size = .6,alpha = .15,position="jitter") + ## increased size
stat_smooth(se = FALSE, size = .5, alpha = .1, method = "loess") +
scale_color_manual(values=my_palette) +
theme_bw() +
theme(plot.background = element_blank(),
axis.line = element_blank(),
legend.key = element_blank(),
legend.title = element_blank(),
axis.text.x = element_text(size = 8),
axis.text.y = element_text(size = 8)
) +
scale_y_continuous("Value") +
scale_x_continuous("Time", limits=c(0, 100)) +
facet_grid(variable ~ Network_model,scales="free")
Which produces this:
Now, I'd like to selectively set limits for each of the three rows, so that the first row is limits=c(1.9, 3), the second is limits=c(0, 1) and the third is limits=c(.3, .7).
How can I achieve this in ggplot2 of faceting?
I think your best option will be to trim the data before plotting it, e.g. with dplyr,
library(dplyr)
limits <- data.frame(variable = levels(dataM$variable),
min = c(1.9,0,0.3),
max = c(3,1,0.7))
dataC <- inner_join(dataM, limits) %>% filter(value > min, value < max)
last_plot() %+% dataC
(I initially made the points bigger to see the culprits more clearly)
I'm trying to create a framework for easy plotting of our data sets. The current idea is to initiate a ggplot graph, add layers to it, then display or save it. My code looks like this:
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
}
I then call my code like this:
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")
Which produces the following graphic:
As you can see, the legend colors don't match up with the corresponding geom_ribbons, in fact, both are of the color "blue" (found that out by setting the alpha to 1 temporarily). How do I fix this?
Here's the data I want to plot:
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"))
Thanks for sharing your data. Unfortunately as it stands it does not run. GlOBDATA is a list structure and there is no jahr amongst some other omissions.
This answer does not attempt to create a general function or amend yours but hopefully does suggest another way to structure your data.
By restructuring your data, you can map variables to colours and this will automatically produce the legend.
library(ggplot2)
# create dataframe from your list
temp <- do.call(cbind.data.frame, GLOBDATA)
# Change data format
# your data is organised in wide format as mean, upper CI, lower CI (i think)
# for both 'alle' and 'mann'. By stacking these after renaming for consistent
# column names, we can then easily map aesthetics in ggplot.
# create a grouping variable (grp) to map aesthetics to.
df1 <- setNames(temp[grepl('alle', names(temp))], c('mn', 'lower', 'upper'))
df1$grp <- 'alle'
df2 <- setNames(temp[grepl('mann', names(temp))], c('mn', 'lower', 'upper'))
df2$grp <- 'mann'
df <- rbind(df1, df2)
# add year
df$year <- 2000 + seq(nrow(temp))
# plot
p <- ggplot(df, aes(x=year, y=mn , ymin=lower, ymax=upper, colour=grp, fill=grp)) +
geom_line(size = 1, colour="black") +
geom_ribbon(alpha = 0.3, linetype=0) +
scale_x_continuous(breaks = seq(2001, 2012, 1)) +
scale_fill_manual(values=c('alle' = 'red', 'mann'='blue'))
p <- p +
theme(
text = element_text(size=6, face="plain", color="black", family="AvantGarde"),
axis.text = element_text(size=6, face="plain", color="black", family="AvantGarde"),
axis.ticks = element_line(color="black", size=0.5),
axis.line = element_line(color="black", size=0.5),
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",
legend.title=element_blank()
)
So by tweaking how your data is organised and your functions a little you should be able to map variables to aesthetics and automatically generate a legend.