Stacke different plots in a facet manner
First, melt your data to long format.
library(reshape2)
dat.long<-melt(dat,id.vars="time")
head(dat.long)
time variable value
1 0 surv 1.0000000
2 5 surv 0.9956140
3 11 surv 0.9824561
4 12 surv 0.9780702
5 13 surv 0.9692982
6 15 surv 0.9649123
Then use subset()
to use only surv
data in geom_step()
and nr
data in geom_area()
and with facet_grid()
you will get each plot in separate facet as variable
is used to divide data for facetting and for subsetting. scales="free_y"
will make pretty axis.
ggplot()+geom_step(data=subset(dat.long,variable=="surv"),aes(time,value))+
geom_area(data=subset(dat.long,variable=="nr"),aes(time,value))+
facet_grid(variable~.,scales="free_y")
Stacking calculated plots in a facet manner
you could make a dummy facetting variable,
d <- lattice::make.groups(first=df, second=df)
ggplot(data=d, aes(x=player, fill=is.winner)) +
facet_grid(which~., scales="free") +
geom_bar(position = "stack", data=subset(d, which=="first")) +
geom_bar(position = 'fill', data=subset(d, which=="second")) +
scale_fill_brewer(palette="Set2")
interestingly, flipping the coordinates produces an incorrect plot, at least with the dev version of ggplot2.
last_plot() + coord_flip()
GGPlot: Different chart types in facets?
Following our comments, I would probably do something like this:
new_dat <- melt(list(trial.df,trial.df),id.vars = 1:2)
new_dat$trial.id <- factor(new_dat$trial.id)
new_dat$surv <- factor(new_dat$surv)
ggplot() +
facet_wrap(~L1,nrow = 2,scales = "free_y") +
geom_bar(data = subset(new_dat,L1 == 1),
aes(x = trial.id , fill = surv),
stat = "bin",
position = "fill") +
geom_bar(data = subset(new_dat,L1 == 2),
aes(x = trial.id , fill = surv),
stat = "bin") +
ggtitle("Surival by Trial\n") +
labs(x = "\nTrial", y = "Patients (%)\n") +
scale_fill_discrete(name = "Survival")
(And ignore the warnings.)
Adding plots in between faceted plots with ggplot
There is a function grid.arrange()
from gridExtra
package (nice explanation [here])1. This allows you to take separately plotted plots and then arrange them in a consistent grid and adjust the size of each plot individually if needed. For this to work you would need to plot each one of your facets separately and then pass them to the package.
Assuming that each of your facets is called something like A1 for top left, B4 for bottom left. Your code would look something like this:
grid.arrange(
grobs = c(Al:B4)
widths = c(2, 1, 2, 1),
layout_matrix = rbind(c(1, 2, 3, 4),
c(5, 6, 7, 8))
)
Using this format, you would individually label your plots so as to overcome the difference in types of scale.
Different function curves for each facet in ggplot2
And with many thanks to comment from @Roland
If we add to toy.data
predicted data from the function predict.hedgehogs
above:
pred.hogs <- predict.hedgehogs(year, rate)
toy.data <- data.frame(toy.data, pred.hogs)
We can plot:
ggplot(toy.data, aes(year, hedgehogs)) +
geom_point(aes(colour=queen), size=10) +
scale_colour_manual(values=c("red", "white")) +
facet_grid(rate ~ site) +
geom_smooth(aes(x=year, y=pred.hogs), stat="identity", colour = "black")
How to use 'facet' to create multiple density plot in GGPLOT
You'll have to prepare your data first. I've illustrated this on your data.frame df
as it is a proper normal distribution.
require(ggplot2)
require(reshape2)
df$id <- 1:nrow(df)
df.m <- melt(df, "id")
df.m$grp1 <- factor(gsub("\\..*$", "", df.m$variable))
df.m$grp2 <- factor(gsub(".*\\.", "", df.m$variable))
p <- ggplot(data = df.m, aes(x=value)) + geom_density(aes(fill=grp2), alpha = 0.4)
p <- p + facet_wrap( ~ grp1)
p + scale_fill_brewer(palette = "Set1")
Doing the same by replacing df
with df_log
you'd get something like this:
require(ggplot2)
require(reshape2)
df_log$id <- 1:nrow(df_log)
df.m <- melt(df_log, "id")
df.m$grp1 <- factor(gsub("\\..*$", "", df.m$variable))
df.m$grp2 <- factor(gsub(".*\\.", "", df.m$variable))
p <- ggplot(data = df.m, aes(x=value)) + geom_density(aes(fill=grp2), alpha = 0.5)
p <- p + facet_wrap( ~ grp1)
p
Bar plot with facets and same bar size (binwidth) with option to shrink the panel size
Perhaps an approach using facet grid would be satisfactory:
ggplot(df, aes(x = product, y = customer.satisfaction)) +
geom_bar(stat = "identity", width = 0.9) +
coord_flip() +
facet_grid(store ~., scales = "free", space = "free")
Related Topics
R: Save Multiple Plots from a File List into a Single File (Png or PDF or Other Format)
Offline Installation of R Packages
Ddply Multiple Quantiles by Group
Saving a List of Plots by Their Names()
How to Call the 'Function' Function
R How to Change One of the Level to Na
Calculate Mean by Group Using Dplyr Package
A Way to Access Google Streetview from R
How to Loop Over the Length of a Dataframe in R
Associate a Color Palette with Ggplot2 Theme
Check If R Package Is Installed Then Load Library
How to Pass Pandoc_Args to Yaml Header in Rmarkdown
Linear Interpolate Missing Values in Time Series
How Does One Merge Dataframes by Row Name Without Adding a "Row.Names" Column
Convert a Vector into a List, Each Element in the Vector as an Element in the List