Perfect fit of ggplot2 plot in plot - r

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)

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 NOT to display value 0 in a coord_polar using ggplot2

I'm trying to do a coord_polar plot without display 0,0% values. I've tried this but I have a error (Error: Aesthetics must be either length 1 or the same as the data (2): x, y, label, fill)
a <- c("A", "B", "C")
b <- c(0, 20, 40)
c<- data.frame(a,b)
c$pct <- c$b/sum(c$b) #labels positions
p_c <- ggplot(c, aes(x=0.5, y=b, fill=a)) +
geom_bar(stat="identity", color="white", width=1) +
coord_polar(theta='y') +
theme_bw()+
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text.x=element_blank())+
geom_text(data=subset(c,b >0.05),aes(x=1.2, y=cumsum(c$b) - c$b/2,
label=percent(c$pct)))+
labs(x=NULL, y=NULL)
p_c
Cheers
Do not subset your data. Use ifelse:
ggplot(c, aes(x=0.5, y=b, fill=a)) +
geom_bar(stat="identity", color="white", width=1) +
coord_polar(theta='y') +
theme_bw()+
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text.x=element_blank())+
geom_text(data=c,aes(x=1.2, y=cumsum(c$b) - c$b/2,
label=ifelse(b>0.5,percent(c$pct),"")))+
labs(x=NULL, y=NULL)

ggplot2: Make the filled area of the spatial plot flush with facet strip [duplicate]

I am creating some maps and want to remove all margins between the plot region and panel border.
This is the minimal example to reproduce my question
library(ggplot2)
library(grid)
df <- expand.grid(list(x = seq(1, 10), y = seq(1, 10), z = seq(1, 2)))
p <- ggplot(df) + geom_tile(aes(x, y)) + facet_wrap(~z)
p <- p + theme_minimal() + xlab('') + ylab('')
p <- p + theme(axis.text = element_blank(),
panel.grid = element_blank(),
axis.ticks = element_blank(),
panel.border = element_rect(colour = 'black', fill = 'transparent'),
panel.margin = unit(0, 'mm'))
p + ylim(2, 6) + xlim(2, 6)
This is the result of my codes.
How could I remove all white areas in the figure above? Thanks for any suggestions.
(Alright, here's my comment as an answer..)
Just add the following to the plot:
+ scale_y_continuous(expand = c(0,0)) + scale_x_continuous(expand = c(0,0))

add a common border for combined ggplots

I would like to add a common border for the combined pie charts. When I use panel_border() the left and bottom lines appear as more darker than upper and right lines. I couldn't figure out my mistake.1) How can I add a common border for combined plots?
2) How can I reduce the thickness of left and bottom lines?
df1 <- data.frame(
variable = c("china","korea","canada","UK","USA"),
value = c(1632,1320,4491,991,620)
)
df2 <- data.frame(
variable = c("china","korea","canada","UK","USA"),
value = c(7376,1770,5210,5005,3947)
)
library(ggplot2)
p1 <-ggplot(df1,aes(x="", y = value, fill = variable))+ geom_bar(stat="identity", width=1) + ggtitle("Rainfall - 2014")+ panel_border() +coord_polar(theta = "y")+xlab("")+ylab("")+theme(legend.position="right", legend.title=element_blank(), plot.title = element_text(lineheight=3, face="bold", color="black", size=14))
p2 <-ggplot(df2,aes(x="", y = value, fill = variable))+ geom_bar(stat="identity", width=1) + ggtitle("Rainfall - 2015")+panel_border() +coord_polar(theta = "y")+xlab("")+ylab("")+theme(legend.position="right", legend.title=element_blank(), plot.title = element_text(lineheight=3, face="bold", color="black", size=14))
library(cowplot)
plot_grid(p1, p2, labels=c("A", "B" ))
Sanu,
The first component of the problem is that you are not seeing a border your are seeing the axis.line, and axis.text theme attributes. You need to remove these from the theme by applying element_blank() to both...
library(ggplot2)
library(cowplot)
df1 <- data.frame(
variable = c("china","korea","canada","UK","USA"),
value = c(1632,1320,4491,991,620)
)
df2 <- data.frame(
variable = c("china","korea","canada","UK","USA"),
value = c(7376,1770,5210,5005,3947)
)
p1 <-ggplot(df1, aes(x="", y = value, fill = variable))
p1 <- p1 + geom_bar(stat="identity", width=1)
p1 <- p1 + ggtitle("Rainfall - 2014")
# p1 <- p1 + panel_border()
p1 <- p1 + coord_polar(theta = "y")
p1 <- p1 + xlab("")
p1 <- p1 + ylab("")
p1 <- p1 + theme(legend.position="right",
legend.title=element_blank(),
axis.line=element_blank(),
axis.ticks=element_blank(), # the axis ticks
plot.title = element_text(lineheight=3, face="bold", color="black", size=14))
p2 <-ggplot(df2,aes(x="", y = value, fill = variable))
p2 <- p2 + geom_bar(stat="identity", width=1)
p2 <- p2 + ggtitle("Rainfall - 2015")
# p2 <- p2 + panel_border()
p2 <- p2 + coord_polar(theta = "y")
p2 <- p2 + xlab("")
p2 <- p2 + ylab("")
p2 <- p2 + theme( legend.position="right",
legend.title = element_blank(),
axis.line=element_blank(),
axis.ticks=element_blank(), # the axis ticks
plot.title = element_text(lineheight=3, face="bold", color="black", size=14))
plot_grid(p1, p2, labels=c("A", "B" ))
result:
a better solution might be to clean up the plot as follows:
library(gridExtra)
get_legend<-function(myggplot){
tmp <- ggplot_gtable(ggplot_build(myggplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
return(legend)
}
legend <- get_legend(p1)
p1 <- p1 + theme(legend.position="none")
p2 <- p2 + theme(legend.position="none")
# Arrange ggplot2 graphs with a specific width
grid.arrange(p1, p2, legend, ncol=3, widths=c(2.3, 2.3, 0.8))
result:
and now we add the border by adding...
# next line adds border
grid.rect(.5,.5,width=unit(.99,"npc"), height=unit(0.99,"npc"),
gp=gpar(lwd=3, fill=NA, col="black"))
thus:
to remove the axis test we add axis.text.x=element_blank() to the theme definition... Thus:
library(ggplot2)
library(cowplot)
df1 <- data.frame(
variable = c("china","korea","canada","UK","USA"),
value = c(1632,1320,4491,991,620)
)
df2 <- data.frame(
variable = c("china","korea","canada","UK","USA"),
value = c(7376,1770,5210,5005,3947)
)
p1 <-ggplot(df1, aes(x="", y = value, fill = variable))
p1 <- p1 + geom_bar(stat="identity", width=1)
p1 <- p1 + ggtitle("Rainfall - 2014")
# p1 <- p1 + panel_border()
p1 <- p1 + coord_polar(theta = "y")
p1 <- p1 + xlab("")
p1 <- p1 + ylab("")
p1 <- p1 + theme(legend.position="right",
legend.title=element_blank(),
axis.line=element_blank(),
axis.ticks=element_blank(), # the axis ticks
axis.text.x=element_blank(),
plot.title = element_text(lineheight=3, face="bold", color="black", size=14))
p1
p2 <-ggplot(df2,aes(x="", y = value, fill = variable))
p2 <- p2 + geom_bar(stat="identity", width=1)
p2 <- p2 + ggtitle("Rainfall - 2015")
# p2 <- p2 + panel_border()
p2 <- p2 + coord_polar(theta = "y")
p2 <- p2 + xlab("")
p2 <- p2 + ylab("")
p2 <- p2 + theme( legend.position="right",
legend.title = element_blank(),
axis.line=element_blank(),
axis.ticks=element_blank(), # the axis ticks
axis.text.x=element_blank(),
plot.title = element_text(lineheight=3, face="bold", color="black", size=14))
plot_grid(p1, p2, labels=c("A", "B" ))
library(gridExtra)
get_legend<-function(myggplot){
tmp <- ggplot_gtable(ggplot_build(myggplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
return(legend)
}
legend <- get_legend(p1)
p1 <- p1 + theme(legend.position="none")
p2 <- p2 + theme(legend.position="none")
# 4. Arrange ggplot2 graphs with a specific width
grid.arrange(p1, p2, legend, ncol=3, widths=c(2.3, 2.3, 0.8))
# next line adds border
grid.rect(.5,.5,width=unit(.99,"npc"), height=unit(0.99,"npc"),
gp=gpar(lwd=3, fill=NA, col="black"))
result:

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))

Resources