Pasting ggplot objects horizontally - r

I'm trying to paste 16 different plots into one with a common x axis
I successfully made, plot and paste the graphs individually with grid.draw()
But i haven't been able to transform the x axis in a continuous way or at least remove the space between each graph
i attach some code to illustrate how the graphs were made.
plot1 = ggplot(map_snp %>% filter( chr == chr[1] ), aes(x=POS)) +
geom_histogram( binwidth = 2,
col=palette[2],
fill=palette[2],
alpha = .2) +
xlab("Chromosome 1") +
ylab("SNP count") +
theme_bw() +
theme(axis.text.x = element_text(angle = 0, colour = "black"),
text=element_text(family="Times New Roman", size = 12),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line.y = element_line(colour = "black"),
axis.line.x = element_line(colour = "black"))
par_plots <- list()
for (i in list_par) {
par_plots[[i]] = ggplot(map_snp %>% filter( chr == i ), aes(x=POS)) +
geom_histogram( binwidth = 2,
col= palette[3],
fill= palette[3],
alpha = .2) +
xlab(paste0(i)) +
theme_bw() +
theme(axis.text.x = element_text(angle = 0, colour = "black"),
text=element_text(family="Times New Roman", size = 12),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line.y = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.line.x = element_line(colour = "black"))
}
impar_plots <- list()
for (i in list_impar) {
impar_plots[[i]] = ggplot(map_snp %>% filter( chr == i ), aes(x=POS)) +
geom_histogram( binwidth = 2,
col= palette[2],
fill=palette[2],
alpha = .2) +
xlab(paste0(i)) +
theme_bw() +
theme(axis.text.x = element_text(angle = 0, colour = "black"),
text=element_text(family="Times New Roman", size = 12),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line.y = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.line.x = element_line(colour = "black"))
}
grid.newpage()
png(paste("./snp_map.png"), width = 15*4.5, height = 8, res = 320, units = "cm", pointsize = 12, bg = "white")
grid.draw(cbind(ggplotGrob(plot1),
ggplotGrob(par_plots$`Chromosome 2`),
ggplotGrob(impar_plots$`Chromosome 3`),
ggplotGrob(par_plots$`Chromosome 4`),
ggplotGrob(impar_plots$`Chromosome 5`),
ggplotGrob(par_plots$`Chromosome 6`),
ggplotGrob(impar_plots$`Chromosome 7`),
ggplotGrob(par_plots$`Chromosome 8`),
ggplotGrob(impar_plots$`Chromosome 9`),
ggplotGrob(par_plots$`Chromosome 10`),
ggplotGrob(impar_plots$`Chromosome 11`),
ggplotGrob(par_plots$`Chromosome 12`),
ggplotGrob(impar_plots$`Chromosome 13`),
ggplotGrob(par_plots$`Chromosome 14`),
ggplotGrob(impar_plots$`Chromosome 15`),
ggplotGrob(par_plots$`Chromosome 16`),
size = "last"))
dev.off()
SNP MAP

I can get rid of most of the border, but not quiiiiite all. Here using an example from built in diamonds data set from ggplot2.
par_plots <- list()
cuts = unique(diamonds$cut)
for (c in as.numeric(cuts)) {
par_plots[[c]] = ggplot(diamonds %>% filter(as.numeric(cut) == c),
aes(x = price)) +
geom_histogram() +
scale_x_continuous(expand = expansion(0)) +
xlab(paste0(i)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 0, colour = "black"),
text=element_text(family="Times New Roman", size = 12),
plot.margin = unit(c(0,0,0,0), "cm"),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line.y = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.line.x = element_line(colour = "black"))
}
library(grid)
grid.newpage()
# png(paste("./snp_map.png"), width = 15*4.5, height = 8, res = 320, units = "cm", pointsize = 12, bg = "white")
grid.draw(cbind(ggplotGrob(par_plots[[1]]),
ggplotGrob(par_plots[[2]]),
ggplotGrob(par_plots[[3]]),
ggplotGrob(par_plots[[4]])))
dev.off()

Related

Make the point in the middle of each grid in ggplot2?

Here is the data.
df <- data.frame(x = LETTERS[1:5], y = LETTERS[6:10], z = rnorm(5))
Below are the codes to generate a point plot.
ggplot(df, aes(x, y, fill = z, size = z)) +
geom_point(shape = 21, stroke = 1, colour = 'black')
I'm wondering how to make each point into the middle of each grid. Something like this.
I found it worked by adding a geom_tile function.
ggplot(df, aes(x, y)) +
geom_tile(fill = NA, colour = 'black') +
geom_point(aes(fill = z, size = z), shape = 21, stroke = 1, colour = 'black') +
theme(panel.background = element_blank(),
plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line.x = element_blank(),
axis.line.y = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
axis.title = element_text(size = 12),
axis.text.y = element_text(size = 10, colour = 'black'),
axis.text.x = element_text(size = 10, colour = 'black'),
strip.background = element_blank(),
strip.text = element_blank())

R color scatter plot with more color gradiant

Hello I have Dataframe ploting scatter plot with color using other column values plot is looking fine but the colour values are start from 0-100 and most of the values are in between 50-100 so I wanted put more color to diffrentiat can any one suggest me how can I do with the R, I tried with viridis color it is also looking same 50-100 color is almost looking similar color
link for the data
https://drive.google.com/file/d/1EKhRwup3vUC3KVFOOh4XtKERIr8FQj3x/view?usp=sharing
code what I tried
df=read.table("test.txt",sep='\t', header=TRUE)
df = data.frame(df)
p=ggplot(df, aes(log(data1), log(data2)),cex=1.9)+
geom_point(aes(color =data3)) +
theme(legend.position = "top")+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))+
theme(text = element_text(size = 20, face="bold"))
You can play with a customized color palette and scale_colour_gradientn like this:
library(RColorBrewer)
library(ggplot2)
#Data
df <- read.delim(file='test.txt',stringsAsFactors = F)
#Palette
myPalette <- colorRampPalette(rev(brewer.pal(11, "Spectral")))
sc <- scale_colour_gradientn(colours = myPalette(100))
#Plot
ggplot(df, aes(log(data1), log(data2)),cex=1.9)+
geom_point(aes(color =data3)) + sc +
theme(legend.position = "top")+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))+
theme(text = element_text(size = 20, face="bold"))
Output:
If you want more color try this:
#Palette 2
sc2 <- scale_colour_gradientn(colours = rainbow(7))
#Plot
ggplot(df, aes(log(data1), log(data2)),cex=1.9)+
geom_point(aes(color =data3)) + sc2 +
theme(legend.position = "top")+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))+
theme(text = element_text(size = 20, face="bold"))
Output:
Update2: With breaks you can define limits for the color scale:
#Plot 3
ggplot(df, aes(log(data1), log(data2),color=data3),cex=1.9)+
geom_point() +
scale_colour_gradientn(colours = rainbow(25),breaks = seq(0,100,by=5))+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))+
theme(text = element_text(size = 12, face="bold"),
legend.text = element_text(size = 7, face="bold"))
Output:
Update 3: If you want to have different colors you can mix different palettes like this:
#Plot
ggplot(df, aes(log(data1), log(data2),color=data3),cex=1.9)+
geom_point() +
scale_colour_gradientn(colours = c(viridis::inferno(5),
viridis::plasma(5),
viridis::magma(5),
viridis::viridis(5),
rainbow(5)),breaks = seq(0,100,by=5))+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))+
theme(text = element_text(size = 12, face="bold"),
legend.text = element_text(size = 7, face="bold"))
Output:

Use Linetypes and colors examples as axis label in ggplot2

I have a graph with four lines with two different linetypes (solid and dashed) and two colors. I want to display a table below the graph containing some information about each curve, e.g. :
The labels of the y-axis of this table are "S.2", "D.2",...
Is it possible to use labels (for the y-axis) that are examples of what the curve looks like instead of text?
Such as in the legend of the graph?
That is instead of "S.2" having a short blue dashed line?
Here is the code to produce the plot
require(ggplot2)
require(grid)
require(gtable)
## graph
g1<-ggplot() + aes(x=seq(0,5,.1),y=cumsum(sample(0:10,51,replace = T)),colour="1",linetype="S") + geom_line()+
geom_line(aes(x=seq(0,5,.1),y=cumsum(sample(0:10,51,replace = T)),colour="1",linetype="D"))+
geom_line(aes(x=seq(0,5,.1),y=cumsum(sample(0:10,51,replace = T)),colour="2",linetype="S"))+
geom_line(aes(x=seq(0,5,.1),y=cumsum(sample(0:10,51,replace = T)),colour="2",linetype="D"))+
scale_colour_manual(breaks=c("1","2"),values=c("#cd7118","#1874cd"))+
scale_x_continuous(limits = c(0,5),breaks = 0:5,expand=c(0,0))+
scale_linetype_manual(breaks=c("S","D"),values=c("solid","dashed"))+
theme(
axis.line = element_line(size = 0.2, linetype = 'solid',colour = "black"),
axis.text = element_text(size=10),
axis.title = element_blank(),
legend.title = element_blank(),
legend.background = element_blank(),
legend.key = element_blank(),
legend.position = c(.1,.5)
)
## table
count<-data.frame(time=rep(0:5,4),count=rep(0:5,4),col=c(rep("1",12),rep("2",12)),lintype=rep(c(rep("S",6),rep("D",6)),2))
g2<-ggplot(count, aes(x = time , y = interaction(factor(lintype),factor(col)), colour=factor(col), label=count)) +
geom_text(size=4) +
scale_colour_manual(breaks=c("1","2"),values=c("#cd7118","#1874cd"))+
scale_x_continuous(limits = c(0,5),breaks = 0:5,expand=c(0,0))+
theme(
legend.position = "none",
panel.background = element_rect(fill = "white",colour = "white",size = 0.5, linetype = "solid"),
plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
axis.line = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_text(size=10, color = 'black',margin = margin(t = 0, r = 20, b = 0, l = 0)),
axis.ticks=element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
plot.title = element_text(size=10),
plot.margin = margin(t=1,b=1,r=15,l=10,unit="pt")
)
#
merge = rbind(ggplotGrob(g1), ggplotGrob(g2), size="last")
panels <- merge$layout$t[grep("panel", merge$layout$name)]
merge$heights[panels[1]] <- unit(10,"cm")
merge$heights[panels[2]]<-unit(6,"lines")
merge <- gtable_add_rows(merge, heights = unit(1,"line"), 1)
merge$layout$clip[merge$layout$name=="panel"]<-"off"
grid.draw(merge)

The mirror_ticks function not working for ggplot

I want to place ticks on all four sides of my graph. The way suggested for this is mirror_ticks.
library(ggplot2)
library(ggplotTicks)
sp6<-ggplot(Anna_Smooth, aes(y=log10(Prob2), x=log10(AvSize)))+
geom_point( data=Anna_Smooth, aes(y=log10(Prob2), x=log10(AvSize), color=PART) )+
guides( color=FALSE)
sp8<-sp6+ labs(x=expression(paste(log(s))))+
labs(y=expression(paste(log(P(s)))) )+
theme(axis.text.y = element_text(size=14),
axis.text.x = element_text(size=14),
axis.title.y = element_text(size=15),
axis.title.x = element_text(size=15),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
panel.border = element_rect(colour = "black", fill=NA, size=2)
)
sp10<-mirror_ticks(sp10, allPanels=TRUE)
My output sp10 has no ticks on opposite panels, same result if I put allPanels=TRUE
Is there a fix? I am open to learn how one does this with theme settings?
As of ggplot2 version 2.2.0 (2016-11-11), the scale_x_continuous() and scale_y_continuous() can display a secondary axis which is positioned opposite to the primary axis and which can be controlled with the sec.axis argument.
This can be used to mirror the tick marks.
The OP hasn't provided reproducible data so we use the mpg dataset which comes with the ggplot2 package:
Chart without mirrored tick marks
library(ggplot2)
g1 <- ggplot(mpg, aes(log10(displ), log10(hwy))) +
geom_point() +
theme(
axis.text.y = element_text(size = 14),
axis.text.x = element_text(size = 14),
axis.title.y = element_text(size = 15),
axis.title.x = element_text(size = 15),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
panel.border = element_rect(
colour = "black",
fill = NA,
size = 2
)
)
g1
Chart with secondary axes
g1 +
scale_x_continuous(sec.axis = dup_axis()) +
scale_y_continuous(sec.axis = dup_axis())
g1 +
scale_x_continuous(sec.axis = dup_axis(name = NULL)) +
scale_y_continuous(sec.axis = dup_axis(name = NULL))
g1 +
scale_x_continuous(sec.axis = dup_axis(name = NULL, labels = NULL)) +
scale_y_continuous(sec.axis = dup_axis(name = NULL, labels = NULL))
Mirrored tick marks with log10 scales
The secondary axes are also available with the scale_x_log10() and scale_x_log10() functions.
So, it can be avoided to use the log() function within the call to aes() but by specifying an appropriate log scale:
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
theme(
axis.text.y = element_text(size = 14),
axis.text.x = element_text(size = 14),
axis.title.y = element_text(size = 15),
axis.title.x = element_text(size = 15),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
panel.border = element_rect(
colour = "black",
fill = NA,
size = 2
)
) +
scale_x_log10(sec.axis = dup_axis(name = NULL, labels = NULL)) +
scale_y_log10(sec.axis = dup_axis(name = NULL, labels = NULL))

ggplot theme remove y-axis on two plots in gtable, keep on third plot

I am trying to remove the y-axis on two ggplots that are in a gtable with a third ggplot. I would like to show the y-axis for the leftmost graph in the gtable and remove the y-axis completely from the subsequent graphs; however, I would like the x-axis to remain on all plots.
My graph looks like this:
![nucleotide diversity][1]
[1]: image produced by code
library("ggplot2")
library("gridExtra")
library("gtable")
theme_set(theme_bw(base_size=16))
p1 <- ggplot(a.pi, aes(x=window, y=measure, fill=key, colour=key)) +
geom_line() +
scale_colour_manual(values=c("#000099", "#333333", "#FF0000")) +
ylab(expression(pi)) +
xlab("Position") +
scale_x_continuous(breaks=c(1e+06, 2e+06, 3e+06, 4e+06), labels=c("1Mb", "2Mb", "3Mb", "4Mb"))+
scale_y_continuous(limits=c(0.0,0.0004)) +
theme(#axis.text.y = element_blank(),
#axis.ticks.y = element_blank(),
#axis.title.y = element_blank(),
#axis.title.x = element_blank(),
plot.margin = unit(c(0,-3,0,0), "lines"),
plot.background = element_blank(),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
legend.position="none",
axis.line = element_line()
)
p2 <- ggplot(b.pi, aes(x=window, y=measure, fill=key, colour=key)) +
geom_line() +
scale_colour_manual(values=c("#333333", "#FF0000")) +
#ylab(expression(pi)) +
xlab("Position") +
scale_x_continuous(breaks=c(1e+06, 2e+06, 3e+06, 4e+06), labels=c("1Mb", "2Mb", "3Mb", "4Mb"))+
scale_y_continuous(limits=c(0.0,0.0004)) +
theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.title.y = element_blank(),
#axis.title.x = element_blank(),
plot.margin = unit(c(0,-3,0,0), "lines"),
plot.background = element_blank(),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
legend.position="none",
axis.line = element_line()
)
p3 <- ggplot(c.pi, aes(x=window, y=measure, fill=key, colour=key)) +
geom_line() +
scale_colour_manual(values=c("#333333", "#FF0000")) +
#ylab(expression(pi)) +
xlab("Position") +
scale_x_continuous(breaks=c(1e+06, 2e+06, 3e+06, 4e+06), labels=c("1Mb", "2Mb", "3Mb", "4Mb"))+
scale_y_continuous(limits=c(0.0,0.0004)) +
theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.title.y = element_blank(),
#axis.title.x = element_blank(),
plot.margin = unit(c(0,-3,0,0), "lines"),
plot.background = element_blank(),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
legend.position="none",
axis.line = element_line()
)
grid.arrange(p1,p2,p3, nrow=1)
gt1 <- ggplot_gtable(ggplot_build(p1))
gt2 <- ggplot_gtable(ggplot_build(p2))
gt3 <- ggplot_gtable(ggplot_build(p3))
newWidth = unit.pmax(gt1$widths[1:3], gt2$widths[1:3], gt3$widths[1:3])
gt1$widths[1:3] = as.list(newWidth)
gt2$widths[1:3] = as.list(newWidth)
gt3$widths[1:3] = as.list(newWidth)
# New gtable with space for the three plots plus a right-hand margin
gt = gtable(widths = unit(c(1, 1, 1, 0.3), "null"), height = unit(1, "null"))
# Instert gt1, gt2 and gt2 into the new gtable
gt <- gtable_add_grob(gt, gt1, 1, 1)
gt <- gtable_add_grob(gt, gt2, 1, 2)
gt <- gtable_add_grob(gt, gt3, 1, 3)
grid.newpage()
grid.draw(gt)
Your linked image is not showing, but here is my shot in the dark:
Change from this:
plot1 <- theme(#axis.text.y = element_blank(),...
to this:
plot1 <- theme(axis.text.y = element_text(),...
if you want to change the label do this:
plot1 <- ... + ylab("y-axis label")
You need to do two things:
One is set axis.title.x and axis.title.y as blank under options (opts).
The other one is set xlab("") and ylab("")
I have included a code snippet in case it helps:
ggplot(space[1:closetos[i],], aes(dim1, dim9, colour = name,
shape=shape))+ opts(axis.line = theme_segment(colour = "black"),
panel.grid.major = theme_blank(),
panel.grid.minor = theme_blank(),
panel.border = theme_blank(),
panel.background = theme_blank(),
axis.title.x = theme_blank(),
axis.title.y = theme_blank())+
xlab("") +
ylab("")+
theme(text = element_text(size=15, colour = "black"),
axis.text.x = element_text(angle=0, vjust=1, colour = "black"),
axis.text.y = element_text(angle=0, vjust=1, colour = "black"),
axis.line = element_line(colour = 'black', size = 1),
axis.ticks = element_line(colour = 'black', size = 1),
axis.ticks.length = unit(0.3, "cm"),
axis.title.y=element_text(vjust=0.4),
legend.position = "none") +
geom_point(size=5)+
scale_color_manual("Status", values = mycolours) +
xlim((space$dim1[closetos[i]]-0.01), (space$dim1[closetos[i]]+0.01)) +
ylim((space$dim9[closetos[i]]-0.01), (space$dim9[closetos[i]]+0.01))

Resources