Plot multiple matrices in facets with different x-y axis - r

I collected the data from a set of online forums and wanted to plot, using ggplot and facets (one facet per forum), the matrix that represent how many times user A replied to user B.
Here is the code to load a toy example:
library(ggplot2)
library(dplyr)
df.edges <- data.frame(from = c('forum1_user1', 'forum1_user1',
'forum1_user2', 'forum1_user2',
'forum2_user1', 'forum2_user1',
'forum2_user2', 'forum2_user2',
'forum3_user1', 'forum3_user1',
'forum3_user2', 'forum3_user2'),
to = c('forum1_user1', 'forum1_user2',
'forum1_user1', 'forum1_user2',
'forum2_user1', 'forum2_user2',
'forum2_user1', 'forum2_user2',
'forum3_user1', 'forum3_user2',
'forum3_user1', 'forum3_user2'),
weight = 1:12,
timestamp = 1:12,
subforum = c('forum1', 'forum1', 'forum1', 'forum1',
'forum2', 'forum2', 'forum2', 'forum2',
'forum3', 'forum3', 'forum3', 'forum3'))
I try this:
# Sort for later use in scale_discrete
df.edges <- df.edges %>% arrange(timestamp)
gg <- ggplot(df.edges, aes(x = from, y = to, fill = weight)) +
geom_raster() + coord_fixed() +
facet_grid(. ~subforum, scales='fixed') +
scale_x_discrete("from", aes(limits = from))+
scale_y_discrete("to", aes(limits = from)) +
theme_bw() +
theme(axis.line = element_blank(),
axis.text.x = element_text(angle = 90, hjust=1, size=8),
axis.text.y = element_text(hjust=1, size=10),
axis.ticks = element_blank(),
strip.background = element_rect(fill = 'white'),
aspect.ratio = 1) +
ggtitle("Matrix of interactions") + xlab('from') + ylab('to')
print(gg)
which gives this:
And if I set the facet scale scale='free':
However, I want each facet to show only those users belonging to that forum. The matrices should be completely filled with 4 cells in each one.
Any idea?

You could create a separate plot for each level of subforum and then lay them out together using grid.arrange:
library(gridExtra)
library(grid)
First, create the separate plots and store in a list. We add scale_fill_continuous(limits=range(df.edges$weight)) to ensure a consistent fill gradient across the three plots:
pl = lapply(split(df.edges, df.edges$subforum), function(df) {
ggplot(df, aes(x = from, y = to, fill = weight)) +
geom_raster() + coord_fixed() +
facet_grid(. ~subforum, scales='fixed') +
scale_x_discrete("from", aes(limits = from))+
scale_y_discrete("to", aes(limits = from)) +
scale_fill_continuous(limits=range(df.edges$weight)) +
theme_bw() +
theme(axis.line = element_blank(),
axis.text.x = element_text(angle = 90, hjust=1, size=8),
axis.text.y = element_text(hjust=1, size=10),
axis.ticks = element_blank(),
strip.background = element_rect(fill = 'white'),
aspect.ratio = 1) +
xlab('from') + ylab('to')
})
Extract the legend, as we want only one legend, rather than a separate legend for each plot:
# Function to extract legend
#https://github.com/hadley/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs
g_legend<-function(a.gplot){
tmp <- ggplot_gtable(ggplot_build(a.gplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
return(legend) }
# Extract legend as a grob
leg = g_legend(pl[[1]])
Arrange the plots with legend and title:
grid.arrange(
textGrob("Matrix of Interactions"),
arrangeGrob(
arrangeGrob(grobs=lapply(pl, function(x) x + guides(fill=FALSE)), ncol=3),
leg, ncol=2, widths=c(10,1)
),
heights=c(1,20)
)

Related

Displaying R plots in a 1x4 grid, using a shared y-axis

I am trying to display some graphs in a 1x4 grid, but I would like all the graphs to have the same x and y axes.
time maxhgs.sleep_LIPA maxhgs.sed_LIPA maxhgs.stand_LIPA maxhgs.MVPA_LIPA maxhgs.LIPA_MVPA
1 5 0.08289621 0.03241295 0.1129983 0.112998341 -0.01928050
2 10 0.16289049 0.06139545 0.2236818 -0.006728721 -0.04950022
3 15 0.24025861 0.08721203 0.3323473 -0.047756360 -0.08927656
4 20 0.31524160 0.11009218 0.4392581 -0.144261526 -0.13791276
5 25 0.38805152 0.13023596 0.5446498 -0.424789999 -0.19517306
6 30 0.41660977 0.13756729 0.5864293 -0.934884300 -0.26117695
This is the data I am working with.
library(ggplot2)
library(egg)
maxhgs.a <- ggplot(maxhgs.df, aes(time, maxhgs.sleep_LIPA)) + geom_point()+geom_line()
maxhgs.a <- maxhgs.a + scale_x_continuous(name = "Time Reallocated", breaks = seq(5,30, by=5)) +
scale_y_continuous(name = "Change in maxhgs", breaks = seq(0,1, by=0.1))+
ggtitle("Sleep to LIPA")
maxhgs.b <- ggplot(maxhgs.df, aes(time, maxhgs.sed_LIPA)) + geom_point()+geom_line()
maxhgs.b <- maxhgs.b + scale_x_continuous(name = "Time Reallocated", breaks = seq(5,30, by=5)) +
scale_y_continuous(name = "Change in maxhgs", breaks = seq(0,1, by=0.1))+
ggtitle("Sedentary to LIPA")
maxhgs.c <- ggplot(maxhgs.df, aes(time, maxhgs.stand_LIPA)) + geom_point()+geom_line()
maxhgs.c <- maxhgs.c + scale_x_continuous(name = "Time Reallocated", breaks = seq(5,30, by=5)) +
scale_y_continuous(name = "Change in maxhgs", breaks = seq(0,1, by=0.1))+
ggtitle("Standing to LIPA")
maxhgs.d <- ggplot(maxhgs.df, aes(time, maxhgs.MVPA_LIPA)) + geom_point()+geom_line()
maxhgs.d <- maxhgs.d + scale_x_continuous(name = "Time Reallocated", breaks = seq(5,30, by=5)) +
scale_y_continuous(name = "Change in maxhgs", breaks = seq(0.5,-1, by=-0.1))+
ggtitle("MVPA to LIPA")
ggarrange(maxhgs.a,
maxhgs.b +
theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.title.y = element_blank() ),
maxhgs.c +
theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.title.y = element_blank() ),
maxhgs.d +
theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.title.y = element_blank() ),
nrow = 1)
This is what I have attempted so far. This actually "works" in that all the graphs have the same y-axis, but the y-axis doesn't actually reflect what should be on the graphs. As you can see in the graph, the y-axis goes from 0.1 to 0.4, but the maxhgs.d graph should extend from 0.1 to -0.9.
Any advice or suggestions would be greatly appreciated!
You can make this much easier by reshaping your data and using faceting. That way, you only need to define a single plot. This requires you to pivot_longer and change the factor levels to the names you want for each facet, but once this is done, the plot itself is straightforward:
library(ggplot2)
library(dplyr)
# Define the label names for the facets first
labs <- c("LIPA to MVPA", "MVPA to LIPA", "Sedentary to LIPA",
"Sleep to LIPA", "Standing to LIPA")
gg <- maxhgs.df %>%
tidyr::pivot_longer(cols = -1) %>%
mutate(plot = factor(`levels<-`(factor(name), labs), labs[c(4, 3, 5, 2, 1)])) %>%
ggplot(aes(x = time, y = value)) +
geom_line() +
geom_point() +
scale_x_continuous(name = "Time Reallocated") +
scale_y_continuous(name = "Change in maxhgs") +
theme(strip.background = element_blank(),
strip.text = element_text(size = 13))
Now we can either choose to plot with fixed y axes:
gg + facet_grid(.~plot, scale = "fixed")
or with flexible y axes:
gg + facet_wrap(.~plot, scale = "free_y", ncol = 5)
Created on 2020-08-04 by the reprex package (v0.3.0)

Manually change order of y axis items on complicated stacked bar chart in ggplot2

I've been stuck on an issue and can't find a solution. I've tried many suggestions on Stack Overflow and elsewhere about manually ordering a stacked bar chart, since that should be a pretty simple fix, but those suggestions don't work with the huge complicated mess of code I plucked from many places. My only issue is y-axis item ordering.
I'm making a series of stacked bar charts, and ggplot2 changes the ordering of the items on the y-axis depending on which dataframe I am trying to plot. I'm trying to make 39 of these plots and want them to all have the same ordering. I think ggplot2 only wants to plot them in ascending order of their numeric mean or something, but I'd like all of the bar charts to first display the group "Bird Advocates" and then "Cat Advocates." (This is also the order they appear in my data frame, but that ordering is lost at the coord_flip() point in plotting.)
I think that taking the data frame through so many changes is why I can't just add something simple at the end or use the reorder() function. Adding things into aes() also doesn't work, since the stacked bar chart I'm creating seems to depend on those items being exactly a certain way.
Here's one of my data frames where ggplot2 is ordering my y-axis items incorrectly, plotting "Cat Advocates" before "Bird Advocates":
Group,Strongly Opposed,Opposed,Slightly Opposed,Neutral,Slightly Support,Support,Strongly Support
Bird Advocates,0.005473026,0.010946052,0.012509773,0.058639562,0.071149335,0.31118061,0.530101642
Cat Advocates,0.04491726,0.07013396,0.03624901,0.23719464,0.09141056,0.23404255,0.28605201
And here's all the code that takes that and turns it into a plot:
library(ggplot2)
library(reshape2)
library(plotly)
#Importing data from a .csv file
data <- read.csv("data.csv", header=TRUE)
data$s.Strongly.Opposed <- 0-data$Strongly.Opposed-data$Opposed-data$Slightly.Opposed-.5*data$Neutral
data$s.Opposed <- 0-data$Opposed-data$Slightly.Opposed-.5*data$Neutral
data$s.Slightly.Opposed <- 0-data$Slightly.Opposed-.5*data$Neutral
data$s.Neutral <- 0-.5*data$Neutral
data$s.Slightly.Support <- 0+.5*data$Neutral
data$s.Support <- 0+data$Slightly.Support+.5*data$Neutral
data$s.Strongly.Support <- 0+data$Support+data$Slightly.Support+.5*data$Neutral
#to percents
data[,2:15]<-data[,2:15]*100
#melting
mdfr <- melt(data, id=c("Group"))
mdfr<-cbind(mdfr[1:14,],mdfr[15:28,3])
colnames(mdfr)<-c("Group","variable","value","start")
#remove dot in level names
mylevels<-c("Strongly Opposed","Opposed","Slightly Opposed","Neutral","Slightly Support","Support","Strongly Support")
mdfr$variable<-droplevels(mdfr$variable)
levels(mdfr$variable)<-mylevels
pal<-c("#bd7523", "#e9aa61", "#f6d1a7", "#999999", "#c8cbc0", "#65806d", "#334e3b")
ggplot(data=mdfr) +
geom_segment(aes(x = Group, y = start, xend = Group, yend = start+value, colour = variable,
text=paste("Group: ",Group,"<br>Percent: ",value,"%")), size = 5) +
geom_hline(yintercept = 0, color =c("#646464")) +
coord_flip() +
theme(legend.position="top") +
theme(legend.key.width=unit(0.5,"cm")) +
guides(col = guide_legend(ncol = 12)) + #has 7 real columns, using to adjust legend position
scale_color_manual("Response", labels = mylevels, values = pal, guide="legend") +
theme(legend.title = element_blank()) +
theme(axis.title.x = element_blank()) +
theme(axis.title.y = element_blank()) +
theme(axis.ticks = element_blank()) +
theme(axis.text.x = element_blank()) +
theme(legend.key = element_rect(fill = "white")) +
scale_y_continuous(breaks=seq(-100,100,100), limits=c(-100,100)) +
theme(panel.background = element_rect(fill = "#ffffff"),
panel.grid.major = element_line(colour = "#CBCBCB"))
The plot:
I think this works, you may need to play around with the axis limits/breaks:
library(dplyr)
mdfr <- mdfr %>%
mutate(group_n = as.integer(case_when(Group == "Bird Advocates" ~ 2,
Group == "Cat Advocates" ~ 1)))
ggplot(data=mdfr) +
geom_segment(aes(x = group_n, y = start, xend = group_n, yend = start + value, colour = variable,
text=paste("Group: ",Group,"<br>Percent: ",value,"%")), size = 5) +
scale_x_continuous(limits = c(0,3), breaks = c(1, 2), labels = c("Cat", "Bird")) +
geom_hline(yintercept = 0, color =c("#646464")) +
theme(legend.position="top") +
theme(legend.key.width=unit(0.5,"cm")) +
coord_flip() +
guides(col = guide_legend(ncol = 12)) + #has 7 real columns, using to adjust legend position
scale_color_manual("Response", labels = mylevels, values = pal, guide="legend") +
theme(legend.title = element_blank()) +
theme(axis.title.x = element_blank()) +
theme(axis.title.y = element_blank()) +
theme(axis.ticks = element_blank()) +
theme(axis.text.x = element_blank()) +
theme(legend.key = element_rect(fill = "white"))+
scale_y_continuous(breaks=seq(-100,100,100), limits=c(-100,100)) +
theme(panel.background = element_rect(fill = "#ffffff"),
panel.grid.major = element_line(colour = "#CBCBCB"))
produces this plot:
You want to factor the 'Group' variable in the order by which you want the bars to appear.
mdfr$Group <- factor(mdfr$Group, levels = c("Bird Advocates", "Cat Advocates")

R ggplot boxplots varying color and fill

I have a dataset in which I have two groups that underwent test and retest measurements. I created a figure displaying four boxplots (groups x tests) with points for each measurement. Test and retest scores are connected by a line for each subject and the boxplots are colored according to the test or retest session.
Now I would like to fill or un-fill the boxplots according to the group. I have create the figure below by creating two figures (filled and unfilled) by switching the geom_boxplot options in the code below and then merging them in photoshop. However, I was wondering if there is a way to create this figure completely with ggplot?
library(ggplot2)
group <- c("HC","HC","HC","HC","HC","HC","HC","HC","HC","HC","HC","HC","HC","HC","PAT","PAT","PAT","PAT","PAT","PAT","PAT","PAT","PAT","PAT")
session <- c("test","retest","test","retest","test","retest","test","retest","test","retest","test","retest","test","retest","test","retest","test","retest","test","retest","test","retest","test","retest")
value <- c(2,1.998521753,1.874733659,1.718486493,1.623289857,1.546827187,1.423472302,1.391178972,1.706069109,1.633178623,1.55107172,1.529644866,1.85152853,1.955804538,1.642797713,1.618263891,1.332975483,1.191228234,1.314644375,1.18511437,1.881207152,1.764699552,1,1.001585308)
index <- c(1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12)
df <- data.frame(group, session, value, index, U = interaction(session,group))
p <- ggplot(df, aes(x=U, y=value))
p <- p + geom_boxplot(aes(fill=session), data=df, colour="black", outlier.alpha=0.0, lwd=0.8, alpha=0.94)
# p <- p + geom_boxplot(aes(colour=session), data=df, outlier.alpha=0.0, lwd=0.8)
dat <- ggplot_build(p)$data[[1]]
p <- p + geom_segment(data=dat, aes(x=xmin, xend=xmax, y=middle, yend=middle), colour="grey70", size=1.6)
p <- p + stat_summary(fun.y=mean,geom="point",pch="-",colour="grey30",size=8, position = position_dodge(width=0.75))
p <- p + geom_line(aes(group = index), alpha = 0.7, colour ="grey50", data=df)
p <- p + geom_point(size=2, aes(group=session), colour="black", data=df, position = position_dodge(width=0.75))
p <- p + scale_x_discrete(labels=c("HC-test","HC-retest","PAT-test","PAT-retest"))
p <- p + scale_y_continuous(limits=c(0.9,2.1), breaks=c(1,1.5,2))
p <- p + scale_colour_manual(values=c("#bf812d","#35978f"))
p <- p + scale_fill_manual(values=c("#bf812d","#35978f"))
p <- p + theme_bw()
p <- p + theme(
axis.text.x = element_text(colour = "black"),
axis.text.y = element_text(colour = "black"),
axis.title.x = element_blank(),
axis.title.y = element_text(colour = "black"),
legend.position = "none",
panel.border = element_rect(colour = "black", fill=NA, size=1)
)
p <- p + labs(y=expression("Normalized Volume(mm)"^3))
ggsave("~/Desktop/test.pdf", width=5, height=4, units=c("in"), plot=p)
You could try to set the factor order differently. You also need to specify sufficient number of values in scale_xxx_manual. I have stripped down the example to include the boxes only, because this was your focal issue.
df$session <- factor(df$session, levels = c("test", "retest"))
df$U = interaction(df$group, df$session, lex.order = TRUE)
ggplot(df, aes(x = U, y = value, fill = U, color = U)) +
geom_boxplot() +
scale_fill_manual(values = c("white", "white", "#bf812d", "#35978f"), guide = "none") +
scale_color_manual(values = c("#bf812d", "#35978f", "black", "black"), guide = "none")

R: ggplot slight adjustment for clustering summary

Please check my reproducible example and the result chart.
X = t(USArrests)
plot_color_clust = function(X,N=N,
cols=c("red","blue", "orange", "darkgreen","green","yellow","grey","black","white")
){
library(ggplot2)
library(gridExtra)
library(gtable)
library(scales)
library(ggdendro)
library(grid)
library(plyr)
if(N>length(cols)) stop("N too big. Not enough colors in cols.")
if(N>ncol(X)) stop("N too big. Not enough columns in data.")
fit = ClustOfVar::hclustvar(X.quanti = X)
dd.row = as.dendrogram(fit)
ddata_x <- dendro_data(dd.row)
temp = cutree(fit,k=N)
lab <- ggdendro::label(ddata_x)
x=c()
for(i in 1:nrow(lab)){
x[i]= paste( "clust", as.vector(temp[ lab$label[i]==names(temp) ]) ,sep="")
}
lab$group <- x
p1 <- ggplot(segment(ddata_x)) +
geom_segment(aes(x=x, y=y, xend=xend, yend=yend))+coord_flip()+
geom_text(data=lab,
aes(label=label, x=x, y=0, colour=group),hjust=1) +
theme(legend.position="none",
axis.title.y=element_blank(),
axis.title.x=element_blank(),
axis.text.x = element_text(angle = 0, hjust = 0),
axis.title.x = element_text(angle = 0, hjust = 0))+
theme(axis.text = element_blank(), axis.title = element_blank(),
axis.ticks = element_blank(), axis.ticks.margin = unit(0, "lines"),
axis.ticks.length = unit(0, "cm"))+
scale_colour_manual(values=cols)+coord_flip()+
scale_y_continuous(limits = c(-0.1, 2.1))
df2<-data.frame(cluster=cutree(fit,N),states=factor(fit$labels,levels=fit$labels[fit$order]))
df3<-ddply(df2,.(cluster),summarise,pos=mean(as.numeric(states)))
p2 = ggplot(df2,aes(states,y=1,fill=factor(cluster)))+geom_tile()+
scale_y_continuous(expand=c(0,0))+
theme(axis.title=element_blank(),
axis.ticks=element_blank(),
axis.text=element_blank(),
legend.position="none")+coord_flip()+
geom_text(data=df3,aes(x=pos,label=cluster))+
scale_fill_manual(name = "This is my title", values = cols)
gp1<-ggplotGrob(p1)
gp2<-ggplotGrob(p2)
maxHeight = grid::unit.pmax(gp1$heights[2:5], gp2$heights[2:5])
gp1$heights[2:5] <- as.list(maxHeight)
gp2$heights[2:5] <- as.list(maxHeight)
#grid.arrange(gp2, gp1, ncol=2,widths=c(1/6,5/6))
R = arrangeGrob(gp2,gp1,ncol=2,widths=c(1/6,5/6))
R
}
plot_color_clust(X,6)
Questions:
These two parts (left colors tiles and right clustering tree) has inconsistent heights. How do we adjust their heights for them to match each other's?
How can we make the tree on the right side shorter so states names (clustered subjects) can have more space to be fully displayed?
Is there a way make the white space between those two parts smaller?
Your tweaking of the code is appreciated. Thanks.
One major change: Rather than matching heights of the two charts, I extract the plot panel from gp2, then insert it into column 2 of gp1. There are no margins surrounding the resultant gp2, and thus, partly takes care of your point 3.
With respect to point 2: expand the limits of the axis to make room of the labels. (See point 2. in the code below). The parameters for points 2 and 3 were set by trial-and-error. Adjusting one parameter means the other needs to be adjusted.
With respect to point 1: expand the axis using the additive component of exapnd to add half a unit to each end of the axis (See point 1. in the code below).
Minor edit: updating to ggplot2 2.2.0 and R 3.3.2
axis.ticks.margin is deprecated
X = t(USArrests)
plot_color_clust = function(X, N = N,
# cols=c("red","blue", "orange", "darkgreen","green","yellow","grey","black","white")
cols = rainbow(N) # Easier to pick colours
){
library(ggplot2)
library(gtable)
library(grid)
library(ggdendro)
library(plyr)
if(N > length(cols)) stop("N too big. Not enough colors in cols.")
if(N > ncol(X)) stop("N too big. Not enough columns in data.")
fit = ClustOfVar::hclustvar(X.quanti = X)
dd.row = as.dendrogram(fit)
ddata_x <- dendro_data(dd.row)
temp = cutree(fit, k = N)
lab <- ggdendro::label(ddata_x)
x = c()
for(i in 1:nrow(lab)){
x[i] = paste("clust", as.vector(temp[lab$label[i] == names(temp)]), sep = "")
}
lab$group <- x
p1 <- ggplot(segment(ddata_x)) +
geom_segment(aes(x = x, y = y, xend = xend, yend = yend)) +
geom_text(data = lab, aes(label = label, x = x, y = -.05, colour = group), # y = -.05 adds a little space between label and tree
size = 4, hjust = 1) +
scale_x_continuous(expand = c(0, .5)) + # 1. Add half a unit to each end of the vertical axis
expand_limits(y = -0.4) + # 2. Make room for labels
theme_classic() +
scale_colour_manual(values = cols) +
coord_flip() +
theme(legend.position = "none", axis.line = element_blank(),
axis.text = element_blank(), axis.title = element_blank(),
axis.ticks = element_blank(),
axis.ticks.length = unit(0, "cm"))
df2 <- data.frame(cluster = cutree(fit, N),
states = factor(fit$labels, levels = fit$labels[fit$order]))
df3 <- ddply(df2, .(cluster),summarise,pos=mean(as.numeric(states)))
p2 <- ggplot(df2, aes(states, y = 1,
fill = factor(as.character(cluster)))) + # 'as.character' - so that colours match with 10 or more clusters
geom_tile() +
scale_y_continuous(expand = c(0, 0)) +
scale_x_discrete(expand = c(0, 0)) +
coord_flip() +
geom_text(data = df3,aes(x = pos, label = cluster, size = 12)) +
scale_fill_manual(values = cols)
gp1 <- ggplotGrob(p1) # Get ggplot grobs
gp2 <- ggplotGrob(p2)
gp2 <- gp2[6, 4] # 3. Grab plot panel only from tiles plot (thus, no margins)
gp1 <- gtable_add_grob(gp1, gp2, t = 6, l = 2, name = "tiles") # 3. Insert it into dendrogram plot
gp1$widths[2] = unit(1, "cm") # 3. Set width of column containing tiles
grid.newpage()
grid.draw(gp1)
}
plot_color_clust(X, 6)

How do I correctly set legend colors in ggplot2 with procedural graph generation?

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.

Resources