Make sure you install ggplot first
example code:
piplot <- ggplot(data, aes(fill=pi, y=pi, x= region)) + geom_errorbar(aes(ymin=pi-pi_sd, ymax=pi+pi_sd), width=.2, position=position_dodge(0.7)) + geom_bar(position="dodge",stat="identity",width=0.7) + coord_flip() + ggtitle("polygenicity") + theme(legend.position = "bottom") + scale_fill_continuous(breaks = c(0.01, 0.03))basic explanation:
name of plot <- ggplot(data, aes(y= values on y, x = value for x) +
geom_bar(position="dodge",stat="identity")
notes about these code:
geom_bar(position"dodge",stat="identity") + coord_flip()
that line of code will flip the bar from vertical to horizontal
ggplot + labs( x= NULL)
no X label, can do the same with y
scale_fill_manual("Column_name", values =
c('name_of_thing' = '#colornumbercode',
'name_of_2nd_thing' = 'colorname'
you can manually est the color of each bar in the bar plot by using this function.
scale_fill_continuous(breaks = c(0.15, 0.35))
changing the labeling of the key/legend, sometimes where the's too many numbers all scrunched in that key/legend
scale fill continuous says to fill in the colors continuously, the numbers in breaks should be where you want the breaks to be with respect to what ever measurement the scale is in.
adjustments that should be made in theme():
theme(axis.title.x= element_text(size = 20),
axis.text.x= element_text(size = 20),
axis.title.y = element_text(size = 20),
axis.text.y = element_text(size = 20),
legend.title = element_text( size = 20),
legend.text = element_text(size = 20),
axis.ticks.length=unit(.25, "cm"),
legend.position = "bottom"),
axis.ticks.y = element_blank()
x axis title
x axis scale label text (the increments number)
y axis title adjustment
y axis numbers/scale text size
the key legend box title
the key legend box inner text
the little tick marks/ "hairy things " on the scale (can get rid of it by inputting NULL
where you want the key box/ legend to be
get rid of the ticks on y axis, same can be done in x axis
Heatmap overview: http://compbio.ucsd.edu/making-heat-maps-r/
Heatmap annotation: https://www.datanovia.com/en/lessons/heatmap-in-r-static-and-interactive-visualization/
Example
plot.new()
tiff(paste0(file="/Users/niniliu/Desktop/2020lab/ch-email/heatmap/heatmap.tiff"),
width=1100,height=900)
heatmap(dfMatrix, Colv = NA, Rowv = NA,
col= colorRampPalette(brewer.pal(8, "Blues"))(25),
RowSideColors = c( # grouping row-variables into different colors
rep("paleturquoise4", 12), # area darker turquoise
rep("paleturquoise2", 12))#thickness lighter turquoise
)
dev.off()
Make sure your data is matrix correlation
Manhatten plot also works with normal axis plot settings, axis break. Axis and axis break will be drawn on top
The corrplot package CRAN: https://cran.r-project.org/web/packages/corrplot/vignettes/corrplot-intro.html
this plot displays correlation matrix, confidence interval
only take input data or correlation (-1 to 1)
Example:
M <- cor(mtcars)
corrplot(M,
method = "circle",
type = "upper",
order = "AOE",
addCoef.col = "black",
tl.cex = 3,
number.cex=2.5
)
corrplot.mixed(M)#gives you half number half circle
corrplot.mixed(M, lower.col = "black", number.cex = .7) #changing the number color
corrplot.mixed(M, lower = "ellipse", upper = "circle") #mix different shapes
the cor() function generates the correlation data of whatever data you input
Possible options:
method =
"circle", "square", "ellipse", "number", "shade", "color", "pie"
type =
"full", "upper", "lower"
order =
"AOE", "FPC", "hclust", "alphabet"
addCoef.col = "black",
change the color of the inside numbers if using the "number" option in method
number.cex=2.5
change the size of the inside numbers to 2.5
tl.cex=3,
change the size of the x and y title labels to size 3
put multiple plots side by side/on top of each other use grid view. This sets up an invisible grid boundary.
import gird first
library(grid)
assemble:
grid.newpage()
pushViewport(viewport(layout =grid.layout(1, 3, widths = c(2, 1, 1))))
Grid.layout (#gird on x(horizontal), #grid on y(vertical), widths = how wide each grid is in terms of ratio (in this example the 1st grid is double the size of the next 2 grids)
#put in plots
print(plot1, vp = viewport(layout.pos.row = 1, layout.pos.col = 1))
print(plot2, vp = viewport(layout.pos.row = 1, layout.pos.col = 2))
print(plot3, vp = viewport(layout.pos.row = 1, layout.pos.col = 3))
layout position of row is and col specify which exact row and column to place each plot
Depending on the situation, one might be able to prevent plot cut off by changing the plot.margin() funtion in ggplot's theme()
+ theme(plot.margin=unit(c(0.4,0,0,0.3),"cm"), #top,right,bottom,left)
Depending on what the problem is, it might also be helpful to change the plot margin to reduce space between axis titles and the plot, example of reducing space between y-axis lable and the plot:
theme(axis.title.y = element_text(size = 40,margin = margin(t = 0, r = -40, b = 0, l = 0))) #font size 40, reducing space of right margin for the plot to be close to the text/yaxis title on the right.
can try adding: par(mar=c(5,6,4,1)+.1)
margins : https://www.r-graph-gallery.com/74-margin-and-oma-cheatsheet.html
https://www.tenderisthebyte.com/blog/2019/04/25/rotating-axis-labels-in-r/
axis and text: https://www.statmethods.net/advgraphs/axes.html
how to display all x labels: https://stackoverflow.com/questions/9981929/how-to-display-all-x-labels-in-r-barplot
qqman intro: https://cran.r-project.org/web/packages/qqman/vignettes/qqman.html
las = rotate labels text
just adding text on bottom mtext(text="Colors", side=1, line=6.5)
Save non ggplots
axis() example code
axis(2,at=c(0,10,20,30,31.5,39),labels=c(0,10, 20, 30,40,60),las = 2, cex.axis=4, font=2, mgp = c(4, 3, 0))
axis(1, at= chrmaxpos$labpos, labels=1:22, pos = 0, cex.axis=2, col= NULL, col.ticks = NULL,
mgp = c(3, 3, 0), font=2, lwd=4)
Quick and dirty explanation:
axis(x or y side, at=where you want the marks to be, labelss= how you want the marks to be labeled, las = bold font, cex.axis= font size of the measurement mark, font = label font (?), mgp=c(location of label, tick mark label, tick mark))
see more about mgp: http://rfunction.com/archives/1302
lwd = thickness of line
xaxt="n"
tck = 0 no ticks
Riverplot
complex heatmap
installation
library(devtools)
install_github("jokergoo/ComplexHeatmap")
https://jokergoo.github.io/ComplexHeatmap-reference/book/introduction.html
moving heatmap
https://stackoverflow.com/questions/15660363/how-to-change-position-of-rowsidecolors-in-r-heatmap-2
Example
sink("summary.txt")
print(summary(df))
sink()
This will print exactly what is in the console of the summary() onto the .txt file.