fill points based on date ranges - r

I am trying to expand a previous answer on filling a histogram based on date cuts to coloring points based on the same cuts.
library(ggplot2)
library(lubridate)
library(scales)
# random dates
# https://stackoverflow.com/questions/14720983/efficiently-generate-a-random-sample-of-times-and-dates-between-two-dates
randdate <- function(N, st="2012/01/01", et="2012/12/31") {
st <- as.POSIXct(as.Date(st))
et <- as.POSIXct(as.Date(et))
dt <- as.numeric(difftime(et,st,unit="sec"))
ev <- sort(runif(N, 0, dt))
rt <- st + ev
}
set.seed(42)
dat <- data.frame(y=sample(c(0:50), 1000, replace=TRUE),
date=randdate(1000))
dat$date <- ymd(substr(dat$date, 1, 10))
ggplot(dat,
aes(x=date, y=y,
fill=cut(..x..,
breaks=c(min(..x..), as.POSIXct("2012-03-01"),
as.POSIXct("2012-04-28"), max(..x..)),
labels=c("before","during","after"),
include.lowest=TRUE))) +
geom_point() +
scale_x_datetime(labels=date_format("%m-%Y"),
breaks=date_breaks("1 year")) +
scale_fill_manual(values=c("#E69F00", "#56B4E9", "#009E73"))
Also an attempt based on this answer, but with fewer "cuts".
ggplot(dat) +
geom_point(aes(x=date, y=y,
colour=x > as.POSIXct("2012-03-01"))) +
geom_line(colour="#999999", size=1) +
scale_colour_manual(values=c("#56B4E9", "#009E73")) +
ylab("Count") +
theme_bw() +
theme(axis.title.x = element_blank(),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
axis.line = element_line(color = 'black'),
title=element_text(size=9, face="bold"),
legend.position="none")

First, geom_point uses the color aesthetic, not fill. Next, I find that it's makes more sense (to me) to assign the factored colors to the data frame vs try to make the ggplot calls more complex:
dat$col <- "during"
dat$col <- ifelse(dat$date < as.POSIXct("2012-03-01"), "before", dat$col)
dat$col <- ifelse(dat$date > as.POSIXct("2012-04-28"), "after", dat$col)
dat$col <- factor(dat$col, c("before", "during", "after"), ordered=TRUE)
ggplot(dat, aes(x=date, y=y, color=col)) +
geom_point() +
scale_x_datetime(labels=date_format("%m-%Y"),
breaks=date_breaks("1 year")) +
scale_color_manual(values=c("#E69F00", "#56B4E9", "#009E73"))

Related

Add a specific color to each tile and a legend in geom_tile

I want to add two things to the following figure:
add a legend (zp or df$legend) (which is similar to each block) to the geom_tile figure
assign a specific color to each tile (which repeats for every block) instead of scale_fill_continuous but keep the "white" color for NA values.
library(ggplot2)
library(RColorBrewer)
xp <- c('Disease_1','Disease_2','Disease_3','Disease_4')
yp <- c('B Cell','T Cell')
zp <- c('feature1','feature2','feature3','feature4','feature5','feature6','feature7','feature8')
xp1 <- xp[1:2]
df <- list(x=1:2,y=1:4,xp1,yp)
df <- expand.grid(df)
df$z <- c(1.804344554,1.158037086,1.686173307,0.500280283,1.710806067,0.857513435,0.66474755,1.164780941,1.769090931,2.058400169,3.114233859,1.436684123,1.770306398,0.995507604,2.538556363,2.264486118,1.424789875,1.816608927,2.773082903,1.197434618,0.829416784,1.622892741,2.035117094,1.650363345,1.927235048,1.546477438,2.308773122,1.041881013,1.216029616,0.478353441,0.834348006,1.240448774)
df$legend <- rep(c('feature1','feature2','feature3','feature4','feature5','feature6','feature7','feature8'))
cols <- rev(brewer.pal(11, 'RdYlBu'))
p <- ggplot(df)
p <- p + geom_tile(aes(x, y, fill = ifelse(z > 1, z, NA)),
colour = "black", width=0.85, height=0.85, size=1) +
scale_fill_continuous(name = "z Scale", na.value = 'white') +
labs() +
facet_grid(Var3~Var4) +
theme(strip.background =element_blank(), #remove bg color of facet_grid texts
strip.text.x = element_text(size=12, color="black", face="bold"),
strip.text.y = element_text(size=10, color="black", face="bold"))
print(p)
p + coord_fixed() +
theme(
axis.title = element_blank(),
axis.text=element_blank(),
axis.ticks=element_blank(),
plot.background=element_blank(),
panel.border=element_blank(),
panel.background = element_blank())
Thanks in advance.

How to customize the X axis of two factors and colour bars based on two factors

I've produced a plot with data showing dislodgement at two different sites based on two main factors (Season and Exposure) which are both labelled in the X axis. I'd like to edit the X axis for seasons so it is not in alphabetical order but labelled from Spring - Winter (instead of Autumn to Winter). On the second row showing Exposure; the are two sets of Exposed labels; I can see that this is because it is being centered in one every three labels of the Season labels but can't seem to correct it.
If possible too, I would like to change the colours of my bars based on season with a lighter shade of the same colour for the second site. Ideally, green for Spring, Yellow for Summer, Brown for Autumn and Grey for Winter, and place ticks in between the seasons and a longer tick in between exposure
desired colour coded bars output from excel
I've tried using this code;
Season <- as.character(data$Season)
#Then turn it back into a factor with the levels in the correct order
Season <- factor(data$Season), levels=unique(data$Season)
To correct the alphabetical order for Seasons but it does nothing, even after I have corrected the labels in my csv. file to the correct order.
This is my full code that I'm using at the moment that was kindly provided for me by a stack overflow user on a previous question.
output from R using current code
library(ggplot2)
library(gtable)
library(grid)
Season <- (data$Season)
Site <- (data$Site)
Exposure <- (data$Exposure)
Average <- data$Average
SEM <- data$SEM
gg <- ggplot(aes(x=as.factor(Site), y=Average, fill=as.factor(Site)), Season, data = data)
gg <- gg + geom_bar(stat = 'identity')
gg <- gg + scale_fill_discrete(guide_legend(title = 'Site'))
gg <- gg + scale_fill_manual(values=c('black', 'grey85'), guide_legend(title = 'Site'))
gg <- gg + theme_classic()
gg <- gg + geom_errorbar(aes(ymin=Average-SEM, ymax=Average+SEM), width=.3)
gg <- gg + facet_wrap(~Exposure*Season, strip.position=c('bottom'), nrow=1, drop=F)
gg <- gg + scale_y_continuous(expand = expand_scale(mult = c(0, .05)))
gg <- gg + theme(axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.x = element_blank(),
axis.line = element_line(color='black'),
strip.placement = 'outside',
panel.spacing.x=unit(0, "lines"),
panel.grid.major.x = element_blank(),
panel.grid = element_blank(),
panel.background = element_rect(fill='white'),
strip.background = element_rect(fill='white', color='white')
)
print(gg)
season.levels <- levels(data$Season)
exposure.levels <- levels(data$Exposure)
g <- ggplotGrob(gg)
grob.numbers <- grep("strip-b", g$layout$name)
b.strips <- gtable_filter(g, "strip-b", trim = FALSE)
season.left.panels <- seq(1, by=length(levels(data$Exposure)), length.out = length(season.levels))
season.right.panels <- seq(length(exposure.levels), by=length(exposure.levels), length.out = length(season.levels))
left <- b.strips$layout$l[season.left.panels]
right <- b.strips$layout$r[season.right.panels]
top <- b.strips$layout$t[3]
bottom <- b.strips$layout$b[3]
mat <- matrix(vector("list", length = 10), nrow = 2)
mat[] <- list(zeroGrob())
for (i in 1:length(season.levels)) {
res <- gtable_matrix("season.strip", mat, unit(c(1, 0, 1, 0, 1), "null"), unit(c(1, 1), "null"))
season.left <- season.left.panels[i]
res <- gtable_add_grob(res, g$grobs[[grob.numbers[season.left]]]$grobs[[3]], 2, 1, 2, 5)
for (j in 0:2) {
exposure.x <- season.left+j
res$grobs[[c(1, 5, 9)[j+1]]] <- g$grobs[[grob.numbers[exposure.x]]]$grobs[[3]]
}
new.grob.name <- paste0(levels(data$Season)[i], '-strip')
g <- gtable_add_grob(g, res, t = top, l = left[i], b = top, r = right[i], name = c(new.grob.name))
new.grob.no <- grep(new.grob.name, g$layout$name)[3]
g$grobs[[new.grob.no]]$grobs[[nrow(g$grobs[[new.grob.no]]$layout)]]$children[[3]]$children[[3]]$gp <- gpar(fontface='bold')
}
grid.newpage()
grid.draw(g)
You can use the alpha aesthetic to get different shades of the color per Site and then manually assign your desired colors:
data$Season <- factor(data$Season, levels=c('Spring', 'Summer', 'Autumn', 'Winter'))
data$Site <- as.factor(data$Site)
gg <- ggplot(aes(x=Site, y=Average, fill=Season), data=data)
gg <- gg + geom_bar(stat = 'identity', aes(alpha=Site))
gg <- gg + scale_alpha_manual(values=c(1, .7), guide_legend(title = 'Site'))
gg <- gg + scale_fill_manual(values=c('green', 'yellow', 'brown', 'grey'), guide_legend(title = 'Season')) # to get bars desired colors instead of ggplot's default colors
gg <- gg + theme_classic() # get white background and black axis.line for x- and y-axis
gg <- gg + geom_errorbar(aes(ymin=Average-SEM, ymax=Average+SEM), width=.3)
gg <- gg + facet_wrap(~Season*Exposure, strip.position=c('bottom'), nrow=1, drop=F)
gg <- gg + scale_y_continuous(expand = expand_scale(mult = c(0, .05))) # remove space below zero
gg <- gg + theme(axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.x = element_blank(),
axis.line = element_line(color='black'),
strip.placement = 'outside', # place x-axis above (factor-label-) strips
panel.spacing.x=unit(0, "lines"), # remove space between facets (for continuous x-axis)
panel.grid.major.x = element_blank(), # remove vertical grid lines
# panel.grid = element_blank(), # remove all grid lines
# panel.background = element_rect(fill='white'), # choose background color for plot area
strip.background = element_rect(fill='white', color='white') # choose background for factor labels, color just matters for theme_classic()
)
print(gg)

ggplot: (manually) create legend when referencing different data.frames

Ok, lets take some sample data.
A <- sample(1:100, 25)
B <- sample(1:25, 25)
df.1 <- data.frame(A,B)
C <- sample(1:80, 15)
D <- sample(1:15, 15)
df.2 <- data.frame(C,D)
Then we plot the data using ggplot
library(ggplot2)
(plot2 <- ggplot(NULL) +
geom_point(data=df.1, aes(x=A, y=B),
color='black', cex=1, pch=16 ) +
geom_smooth(data=df.1, aes(x=A, y=B), method="lm", size=1,
se=FALSE, colour="black", linetype=2)+
geom_point(data=df.2, aes(x=C, y=D),
color='black', cex=1, pch=15 ) +
geom_smooth(data=df.2, aes(x=C, y=D), method="lm", size=1,
se=FALSE, colour="black", linetype=1)+
scale_y_continuous("Y scale") +
ggtitle("Plot") +
theme_bw()+
theme(plot.title = element_text(face="bold", size=20),
axis.title.x = element_text(vjust=-0.25),
axis.title.y = element_text(vjust=1),
axis.title = element_text(face="bold", size=15)
)
)
So we have created and modified the title, axis, etc.
But I want to create a legend which shows the linetype's from the geom_smooth() function of df.1 and df.2. It should be in the top right of the graph.
(so for df.1 we want a solid line and df.2 a dashed line)
The example here walks you through an example, but the data comes from within the same data set
Here you go:
#combine and create x and y (as mappings follow
#same pattern)
df.1$group <- "df.1"
df.1$x <- df.1$A
df.1$y <- df.1$B
df.2$group <- "df.2"
df.2$x <- df.2$C
df.2$y <- df.2$D
library(plyr) #for rbind.fill
df.all <- rbind.fill(df.1,df.2)
plot3 <- ggplot(df.all, aes(x=x,y=y,group=group)) +
geom_point(color='black', cex=1, pch=16 ) +
geom_smooth(aes(linetype=group),method="lm", size=1,
se=FALSE, colour="black") +
scale_y_continuous("Y scale") +
ggtitle("Plot") +
theme_bw()+
theme(plot.title = element_text(face="bold", size=20),
axis.title.x = element_text(vjust=-0.25),
axis.title.y = element_text(vjust=1),
axis.title = element_text(face="bold", size=15)
) +
#add custom linetypes (not necessary now, as default mapping to 1 and 2)
plot3 + scale_linetype_manual(values=c("df.1"=1,"df.2"=2))

Perfect fit of ggplot2 plot in plot

I want to plot a restricted cubic spline as main plot and add a box-and-whisker plot to show the variation of the X variable. However, the lower hinge (x=42), the median (x=51), and the upper hinge(x=61) did not fit perfectly with the corresponding grid line of the main plot.
library(Hmisc)
library(rms)
library(ggplot2)
library(gridExtra)
data(pbc)
d <- pbc
rm(pbc)
d$status <- ifelse(d$status != 0, 1, 0)
dd = datadist(d)
options(datadist='dd')
f <- cph(Surv(time, status) ~ rcs(age, 4), data=d)
p <- Predict(f, fun=exp)
df <- data.frame(age=p$age, yhat=p$yhat, lower=p$lower, upper=p$upper)
### 1st PLOT: main plot
(g <- ggplot(data=df, aes(x=age, y=yhat)) + geom_line(size=1))
# CI
(g <- g + geom_ribbon(data=df, aes(ymin=lower, ymax=upper), alpha=0.5, linetype=0, fill='#FFC000'))
# white background
(g <- g + theme_bw())
# X-axis
(breaks <- round(boxplot.stats(p[,"age"])$stats))
(g <- g + scale_x_continuous(breaks=breaks, limits=range(p[,"age"]), labels=round(breaks)))
(g <- g + xlab("Age"))
# Y-Achse
(g <- g + ylab("Hazard Ratio"))
# size and color of axis
(g <- g + theme(axis.line = element_line(color='black', size=1)))
(g <- g + theme(axis.ticks = element_line(color='black', size=1)))
(g <- g + theme( plot.background = element_blank() ))
#(g <- g + theme( panel.grid.major = element_blank() ))
(g <- g + theme( panel.grid.minor = element_blank() ))
(g <- g + theme( panel.border = element_blank() ))
### 2nd PLOT: box whisker plot
describe(df$age, digits=0)
round(range(df$age))
(gg <- ggplot(data=df, aes(x=1, y=age)) + geom_boxplot(outlier.shape=NA, size=1) + coord_flip())
(gg <- gg + theme( axis.line=element_blank() )) #
(gg <- gg + theme( axis.text.x=element_blank() ))
(gg <- gg + theme( axis.text.y=element_blank() ))
(gg <- gg + theme( axis.ticks=element_blank() ))
(gg <- gg + theme( axis.title.x=element_blank() ))
(gg <- gg + theme( axis.title.y=element_blank() ))
(gg <- gg + theme( panel.background=element_blank() ))
(gg <- gg + theme( panel.border=element_blank() )) #
(gg <- gg + theme( legend.position="none" )) #
(gg <- gg + theme( panel.grid.major=element_blank() )) #
(gg <- gg + theme( panel.grid.minor=element_blank() ))
(gg <- gg + theme( plot.background=element_blank() ))
(gg <- gg + theme( plot.margin = unit( c(0,0,0,0), "in" ) ))
(gg <- gg + scale_x_continuous(breaks=c(70,77,84), expand=c(0,0)) )
### FINAL PLOT: put box whisker plot in main plot
(final.gg <- g + annotation_custom(ggplotGrob(gg), ymin=2.4, ymax=2.6))
What do I have to change for the perfect fit?
Is there a better for a automatized alignment of the y-position of the
box-and-whisker?
UPDATE #1
Thanks for your answer! Below you can see my example with your code. However, as you can see, the lower hinge, the median, and the upper hinge still do not fit. What is going wrong?
library(Hmisc)
library(rms)
library(ggplot2)
library(gridExtra)
data(pbc)
d <- pbc
rm(pbc, pbcseq)
d$status <- ifelse(d$status != 0, 1, 0)
dd = datadist(d)
options(datadist='dd')
f <- cph(Surv(time, status) ~ rcs(age, 4), data=d)
p <- Predict(f, fun=exp)
df <- data.frame(age=p$age, yhat=p$yhat, lower=p$lower, upper=p$upper)
### 1st PLOT: main plot
(breaks <- boxplot.stats(p[,"age"])$stats)
g <- ggplot(data=df, aes(x=age, y=yhat)) + geom_line(size=1) +
geom_ribbon(data=df, aes(ymin=lower, ymax=upper), alpha=0.5, linetype=0, fill='#FFC000') +
theme_bw() +
scale_x_continuous(breaks=breaks) +
xlab("Age") +
ylab("Hazard Ratio") +
theme(axis.line = element_line(color='black', size=1),
axis.ticks = element_line(color='black', size=1),
plot.background = element_blank(),
# panel.border = element_blank(),
panel.grid.minor = element_blank())
### 2nd PLOT: box whisker plot
gg <- ggplot(data=df, aes(x=1, y=age)) +
geom_boxplot(outlier.shape=NA, size=1) +
scale_y_continuous(breaks=breaks) +
ylab(NULL) +
coord_flip() +
# theme_bw() +
theme(axis.line=element_blank(),
# axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
axis.title=element_blank(),
# panel.background=element_blank(),
panel.border=element_blank(),
# panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
# plot.background=element_blank(),
plot.margin = unit( c(0,0,0,0), "in" ),
axis.ticks.margin = unit(0, "lines"),
axis.ticks.length = unit(0, "cm"))
### FINAL PLOT: put box whisker plot in main plot
(final.gg <- g + annotation_custom(ggplotGrob(gg), ymin=2.4, ymax=2.6))
Minor edit: Updating to ggplot2 2.0.0
axis.ticks.margin is deprecated
In the boxplot, even though you have set various elements to element_blank and margins to zero, default spaces remain resulting in the misalignment. These spaces belong to:
axis.ticks.length
xlab
In the code below, I've re-arranged your code somewhat (I hope that's okay), and commented out some lines of code so that it can be seen that the two plots do align. I've also set the breaks in the two plot to un-rounded breaks (min and max values, hinges, and the median).
# X-axis
(breaks <- boxplot.stats(p[,"age"])$stats)
### 1st PLOT: main plot
g <- ggplot(data=df, aes(x=age, y=yhat)) + geom_line(size=1) +
geom_ribbon(data=df, aes(ymin=lower, ymax=upper), alpha=0.5, linetype=0, fill='#FFC000') +
theme_bw() +
scale_x_continuous(breaks=breaks) +
xlab("Age") +
ylab("Hazard Ratio") +
theme(axis.line = element_line(color='black', size=1),
axis.ticks = element_line(color='black', size=1),
plot.background = element_blank(),
# panel.border = element_blank(),
panel.grid.minor = element_blank())
### 2nd PLOT: box whisker plot
gg <- ggplot(data=df, aes(x=1, y=age)) +
geom_boxplot(outlier.shape=NA, size=1) +
scale_y_continuous(breaks=breaks) +
xlab(NULL) +
coord_flip() +
# theme_bw() +
theme(axis.line=element_blank(),
# axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
axis.title=element_blank(),
# panel.background=element_blank(),
panel.border=element_blank(),
# panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
# plot.background=element_blank(),
plot.margin = unit( c(0,0,0,0), "in" ),
# axis.ticks.margin = unit(0, "lines"),
axis.ticks.length = unit(0, "cm"))
### FINAL PLOT: put box whisker plot in main plot
(final.gg <- g + annotation_custom(ggplotGrob(gg), ymin=2.4, ymax=2.6))
You should note that ggplot's method for calculating the hinges differs slightly from the method used by boxplot.stats.
# ggplot's hinges
bp = ggplot(data=df, aes(x=1, y=age)) +
geom_boxplot(outlier.shape=NA, size=1)
bpData = ggplot_build(bp)
bpData$data[[1]][1:5]
Alternative answer using gtable functions to position the boxplot outside the main plot. The height of the box plot can be adjusted using the "h" parameter
library(rms)
# Data
data(pbc)
d <- pbc
rm(pbc)
d$status <- ifelse(d$status != 0, 1, 0)
dd = datadist(d)
options(datadist='dd')
f <- cph(Surv(time, status) ~ rcs(age, 4), data=d)
p <- Predict(f, fun=exp)
df <- data.frame(age=p$age, yhat=p$yhat, lower=p$lower, upper=p$upper)
# X-axis
breaks <- boxplot.stats(p[,"age"])$stats
# Main plot
MP <- ggplot(data=df, aes(x=age, y=yhat)) + geom_line(size=1) +
geom_ribbon(data=df, aes(ymin=lower, ymax=upper), alpha=0.5, linetype=0, fill='#FFC000') +
theme_bw() +
scale_x_continuous(breaks=breaks) +
xlab("Age") +
ylab("Hazard Ratio") +
theme(axis.line = element_line(color='black', size=1),
axis.ticks = element_line(color='black', size=1),
panel.grid.minor = element_blank())
# Boxplot
BP <- ggplot(data=df, aes(x=factor(1), y=age)) +
geom_boxplot(width = 1, outlier.shape=NA, size=1) +
geom_jitter(position = position_jitter(width = .3), size = 1) +
scale_y_continuous(breaks=breaks) +
coord_flip() +
theme_bw() +
theme(panel.border=element_blank(),
panel.grid=element_blank())
#### Set up the grobs and gtables here
library(gtable)
library(grid)
h = 1/15 # height of boxplot panel relative to main plot panel
# Get ggplot grobs
gMP = ggplotGrob(MP)
BPg = ggplotGrob(BP)
BPg = gtable_filter(BPg, "panel") # from the boxplot, extract the panel only
# In the main plot, get position of panel in the layout
pos = gMP$layout[gMP$layout$name == "panel", c('t', 'l')]
# In main plot, set height for boxplot
gMP$heights[pos$t-2] = unit(h, "null")
# Add boxplot to main plot
gMP = gtable_add_grob(gMP, BPg, t=pos$t-2, l=pos$l)
# Add small space
gMP$heights[pos$t-1] = unit(5, "pt")
# Draw it
grid.newpage()
grid.draw(gMP)

Saving grid.arrange plot with my function

I wrote a function for saving ggplots:
fun.save <- function(my.plot, my.plot.name, width, height){
name <- deparse(substitute(my.plot.name))
name <- paste(name, ".pdf", sep="")
cairo_pdf(name, width=width, height=height)
print(my.plot)
dev.off()
}
I used grid.arrange to create one plot from 2 others:
#create data frame
state <- c("USA", "Szwajcaria", "Kanada", "Japonia", "Korea Płd.", "Meksyk")
state <- as.factor(state)
gdp <- c(35, 30, 28, 26, 15, 9)
expenditure <- c(7.75, 8, 6, 6, 3.75, 1.5)
df.gdp <- data.frame(state, gdp, expenditure)
df.gdp$state <- factor(df.gdp$state, levels=df.gdp[order(df.gdp$gdp), "state"])
#create two bar plots
plot.tmp <- ggplot(df.gdp, aes(x=1, y=state)) +
geom_text(aes(label=state)) +
ggtitle("") +
ylab(NULL) +
scale_x_continuous(expand=c(0,0),limits=c(0.94, 1.065)) +
theme(axis.title=element_blank(),
panel.grid=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
panel.background=element_blank(),
axis.text.x=element_text(color=NA),
axis.ticks.x=element_line(color=NA),
plot.margin = unit(c(1,-1,1,-1), "mm"))
plot.gdp <- ggplot(data = df.gdp, aes(x = state, y = gdp)) +
xlab(NULL) +
geom_bar(stat = "identity") +
ggtitle("PKB na osobę (w tys. $)") +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank()) +
scale_y_reverse() +
coord_flip()
plot.exp <- ggplot(data = df.gdp, aes(x = state, y = expenditure)) +
xlab(NULL) +
geom_bar(stat = "identity") +
ggtitle("Roczny wydatek na studenta (w tys. $)") +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank() ) +
coord_flip()
#merge plots
plot.gdp <- ggplot_gtable(ggplot_build(plot.gdp))
plot.exp <- ggplot_gtable(ggplot_build(plot.exp))
plot.tmp<- ggplot_gtable(ggplot_build(plot.tmp))
I can save it in this way:
pdf("GnpexpBad.pdf", width=10, height=5)
grid.arrange(plot.gdp, plot.tmp, plot.exp, ncol=3, widths=c(0.35, 0.078, 0.35))
dev.off()
But I want to use my function to do it. I call my function:
myplot <- grid.arrange(plot.gdp, plot.tmp, plot.exp, ncol=3, widths=c(0.35, 0.078, 0.35))
fan.save(myplot, nameofmyplot, 10,5)
Unfortunately it doesn't work. Any idea how to rearrange function to make it work also for grid plots?

Resources