bar and line plot in one chart with a legend under ggplot2 - r

I would like put a bar and a line plot of two separate but related series on the same chart with a legend (the bar plot is of quarterly growth the line plot is of annual growth).
I currently do it with a data.frame in wide format and code like this:
p <- ggplot() +
geom_bar(df, aes(x=Date, y=quarterly), colour='blue') +
geom_line(df, aes(x=Date, y=annual), colour='red')
but I cannot work out how to add a legend, which has a red line labeled 'Annual Growth'; and a blue square labeled 'Quarterly Growth'.
Alternatively, I cannot work out how to have differnt geoms for different series with a long-form data.frame.
UPDATE:
The following example code gets me part of the way towards a solution, but with a really ugly duplicate legend. Still looking for a complete solution ... This approach is based on putting the data in long form and then plotting subsets of the data ...
library(ggplot2)
library(reshape)
library(plyr)
library(scales)
### --- make a fake data set
x <- rep(as.Date('2012-01-01'), 24) + (1:24)*30
ybar <- 1:24
yline <- ybar + 1
df <- data.frame(x=x, ybar=ybar, yline=yline)
molten <- melt(df, id.vars='x', measure.vars=c('ybar', 'yline'))
molten$line <- ifelse(molten$variable=='yline', TRUE, FALSE)
molten$bar <- ifelse(molten$variable=='ybar', TRUE, FALSE)
### --- subset the data set
df.line <- subset(molten, line==TRUE)
df.bar <- subset(molten, bar==TRUE)
### --- plot it
p <- ggplot() +
geom_bar(data=df.bar, mapping=aes(x=x, y=value, fill=variable, colour=variable),
stat='identity', position='dodge') +
geom_line(data=df.line, mapping=aes(x=x, y=value, colour=variable)) +
opts(title="Test Plot", legend.position="right")
ggsave(p, width=5, height=3, filename='plot.png', dpi=150)
And an example plot ...

By use of the subset argument to geoms.
> x=1:10;df=data.frame(x=x,y=x+1,z=x+2)
> ggplot(melt(df),
aes(x,value,color=variable,fill=variable))+
geom_bar(subset=.(variable=="y"),stat="identity")+
geom_line(subset=.(variable=="z"))

Related

How do I add a separate legend for each variable in geom_tile?

I would like to have a separate scale bar for each variable.
I have measurements taken throughout the water column for which the means have been calculated into 50cm bins. I would like to use geom_tile to show the variation of each variable in each bin throughout the water column, so the plot has the variable (categorical) on the x-axis, the depth on the y-axis and a different colour scale for each variable representing the value. I am able to do this for one variable using
ggplot(data, aes(x=var, y=depth, fill=value, color=value)) +
geom_tile(size=0.6)+ theme_classic()+scale_y_continuous(limits = c(0,11), expand = c(0, 0))
But if I put all variables onto one plot, the legend is scaled to the min and max of all values so the variation between bins is lost.
To provide a reproducible example, I have used the mtcars, and I have included alpha = which, of course, doesn't help much because the scale of each variable is so different
data("mtcars")
# STACKS DATA
library(reshape2)
dat2b <- melt(mtcars, id.vars=1:2)
dat2b
ggplot(dat2b) +
geom_tile(aes(x=variable , y=cyl, fill=variable, alpha = value))
Which produces
Is there a way I can add a scale bar for each variable on the plot?
This question is similar to others (e.g. here and here), but they do not use a categorical variable on the x-axis, so I have not been able to modify them to produce the desired plot.
Here is a mock-up of the plot I have in mind using just four of the variables, except I would have all legends horizontal at the bottom of the plot using theme(legend.position="bottom")
Hope this helps:
The function myfun was originally posted by Duck here: R ggplot heatmap with multiple rows having separate legends on the same graph
library(purrr)
library(ggplot2)
library(patchwork)
data("mtcars")
# STACKS DATA
library(reshape2)
dat2b <- melt(mtcars, id.vars=1:2)
dat2b
#Split into list
List <- split(dat2b,dat2b$variable)
#Function for plots
myfun <- function(x)
{
G <- ggplot(x, aes(x=variable, y=cyl, fill = value)) +
geom_tile() +
theme(legend.direction = "vertical", legend.position="bottom")
return(G)
}
#Apply
List2 <- lapply(List,myfun)
#Plot
reduce(List2, `+`)+plot_annotation(title = 'My plot')
patchwork::wrap_plots(List2)

ggplot2 - facet_wrap with individual legends

I am using ggplot2 to plot maps that have the same extent (i.e. same spatial coverage) but that show different features.
This is how it looks like:
library(raster)
library(reshape2)
library(ggplot2)
# make-up data
r <- raster(system.file("external/test.grd", package="raster"))
s <- stack(r, r**2, r**3, r**4, r**5)
names(s) <- paste0("Field ",seq(1,5))
# convert to data frame
rast.df <- as.data.frame(s, xy=T)
# melt
rast.melt <- melt(rast.df, id.vars = c('x','y'), variable.name="field")
# plot
ggplot() +
geom_raster(data=rast.melt , aes(x=x, y=y, fill=value)) +
facet_wrap(~field) +
scale_fill_continuous(na.value="transparent")
The resulting figure looks quite crappy because there's one single legend for all the maps. Therefore, the maps have no contrast at all.
How can I use individual legends for each facet in the graph above?
Here's an approach with ggarrange from the ggpubr package:
library(ggpubr)
ggarrange(plotlist = lapply(split(rast.melt, rast.melt$field),function(x){
ggplot() + geom_raster(data=x , aes(x=x, y=y, fill=value)) +
scale_fill_continuous(na.value="transparent") +
ggtitle(x$field[1])}))

ggplotly with geom_ribbon grouping

I'm having some problems in converting a ggplot in to a plotly object, and retaining the same legend attributes. What I want:
For grouped series, a single line for fit, and faded region for ribbon of same colour, with transparency
No lines at the edge of the ribbon
Grouped legends for the lines, points and ribbons
Here is the code showing the 2 approaches I tried based on this answer:
ggplot: remove lines at ribbon edges
Both have an undesirable effect as you can see when running. Any suggestions would be great :)
library(plotly)
library(ggplot2)
# fake data
set.seed(1)
dt <- data.frame(x=rep(1:7,2), group=rep(letters[1:2], each=7), value=runif(14))
dt$lwr <- dt$value*.9
dt$upr <- dt$value*1.1
# build plot in ggplot, don't want lines at the edge
pl <- ggplot(data=dt, aes(y=value, x=x, group=group, colour=group,
fill=group)) +
geom_point() +
geom_line() +
geom_ribbon(aes(ymin=lwr, ymax=upr), alpha=.3, linetype=0) +
theme_minimal()
# looks ok, no lines at the edges
pl
# lines at edges, single group
ggplotly(pl)
# alternative: try reverting colour to NA
pl2 <- ggplot(data=dt, aes(y=value, x=x, group=group, colour=group,
fill=group)) +
geom_point() +
geom_line() +
geom_ribbon(aes(ymin=lwr, ymax=upr), alpha=.3, colour=NA) +
theme_minimal()
# looks ok
pl2
# no lines, but now not grouped, and some weird naming
ggplotly(pl2)
Thanks, Jonny
EDIT:
Addition to the accepted answer, in functional form
# dd: ggplotly object
library(stringi)
library(rvest)
remove_ggplotly_ribbon_lines <- function(dd){
find <- rvest::pluck(dd$x$data, "fillcolor")
w <- which(!sapply(find, is.null))
for(i in w){
dd$x$data[[i]]$line$color <-
stringi::stri_replace_all_regex(dd$x$data[[i]]$line$color, ",[\\d.]*\\)$", ",0.0)")
}
return(dd)
}
remove_ggplotly_ribbon_lines(ggplotly(pl))
Hi this is more a comment than an answer but I do not have right to post comments.
If you investigate the ggplotly object you will see that it is actually just a list. Changing the right elements of the list helps in controlling plot options.
The solution below just changes the alpha of the lines at ribbon edges. Hope this helps
library(plotly)
set.seed(1)
dt <- data.frame(x=rep(1:7,2), group=rep(letters[1:2], each=7), value=runif(14))
dt$lwr <- dt$value*.9
dt$upr <- dt$value*1.1
# build plot in ggplot, don't want lines at the edge
pl <- ggplot(data=dt, aes(y=value, x=x, group=group, colour=group,
fill=group)) +
geom_point() +
geom_line() +
geom_ribbon(aes(ymin=lwr, ymax=upr), alpha=.3, linetype=0) +
theme_minimal()
# looks ok, no lines at the edges
pl
# no lines at edges
dd = ggplotly(pl)
dd$x$data[[3]]$line$color = "rgba(248,118,109,0.0)"
dd$x$data[[4]]$line$color = "rgba(0,191,196,0.0)"
dd

Ggplot with more than two legends

I have a data.frame which I'd like to scatter plot using ggplot.
The data have 3 factors whose levels I'd like to show in the legend, although the color of the points will only be according to one of these factors (df$group below).
Here's what I have so far:
set.seed(1)
df <- data.frame(x=rnorm(100),y=rnorm(100),
group=LETTERS[sample(5,100,replace=T)],
type=letters[sample(3,100,replace=T)],
background=sample(4,100,replace=T),stringsAsFactors=F)
df$group <- factor(df$group,LETTERS[1:5])
df$type <- factor(df$type,;etters[1:3])
df$background <- factor(df$background,c(1:4))
I manually specify colors:
require(RColorBrewer)
require(scales)
all.colors <- hcl(h=seq(0,(12-1)/(12),length=12)*360,c=100,l=65,fixup=TRUE)
group.colors <- all.colors[1:5]
type.colors <- all.colors[6:8]
background.colors <- all.colors[9:12]
This is what I have for showing the 3 factors in the legend (df$group and df$type):
require(ggplot2)
ggplot(df,aes(x=x,y=y,colour=group,fill=type,alpha=background))+geom_point(cex=2,shape=1,stroke=1)+
theme_bw()+theme(strip.background=element_blank())+scale_color_manual(drop=FALSE,values=group.colors,name="group")+
guides(fill=guide_legend(override.aes=list(colour=type.colors,pch=0)))
So my question is how to get background.colors appear in the legend under "background" rather than the gray scale colors chosen by default that currently appear there.
ggplot(df,aes(x=x, y=y, colour=group, fill=type, alpha=background))+
geom_point(cex=2, shape=1, stroke=1) +
theme_bw() +
theme(strip.background=element_blank()) +
scale_color_manual(drop=FALSE, values=group.colors, name="group") +
guides(fill=guide_legend(override.aes=list(colour=type.colors,pch=0)),
alpha=guide_legend(override.aes=list(colour=background.colors,pch=0)))

fix the width of the grid in ggplot2

I am trying to plot two side by side plots with ggplot. However, as the two data sets contain different number of observations, the width of the grid is automatically adjusted so that the overall width of the two plots is the same.
However, I need to have the same width between each vertical line-segment on the plot and different total widths to reflect the different sample size in the two cases.
Thanks in advance for any advice.
Here is my code
dat1 <- data.frame(a=1:10,b=letters[1:10])
dat2 <- data.frame(a=1:6, b=letters[12:17])
require(gridExtra)
plot1 <- ggplot(dat1, aes(x=b, y=a)) + geom_point(size=4)
plot2 <- ggplot(dat2, aes(x=b, y=a)) + geom_point(size=4)
grid.arrange(plot1, plot2, ncol=2)
You can set widths in grid.arrange(). The following will automatically adjust the width of the graphs based on the number of levels in each data frame.
I renamed your variables so that row name and elements are not repeated:
dat1 <- data.frame(A1=1:10,B1=letters[1:10])
dat2 <- data.frame(A2=1:6, B2=letters[12:17])
Load the necessary libraries:
require(gridExtra)
require(ggplot2)
Now plot:
plot1 <- ggplot(dat1, aes(x=B1, y=A1)) + geom_point(size=4)
plot2 <- ggplot(dat2, aes(x=B2, y=A2)) + geom_point(size=4)
grid.arrange(plot1, plot2, ncol=2,
widths=c(nlevels(dat1$B1),nlevels(dat2$B2)))
Which gives you the following plot:
As you can see, each level is plotted equidistant from each other across both plots.

Resources