Last active
September 2, 2022 12:29
-
-
Save elowy01/8249d4c8df226940005676e01928da57 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Simple plot example for geom_point | |
data(iris) | |
# where we specify the type of plot with geom_point() | |
# the data to plot with aes | |
IrisPlot <- ggplot(iris, aes(Sepal.Length, Petal.Length, colour=Species))+geom_point() | |
# if we want to change the colour | |
IrisPlot <- ggplot(iris, aes(Sepal.Length, Petal.Length))+geom_point(colour="blue") | |
print(IrisPlot) | |
# Now, let's modify the axes labels and also add title | |
print(IrisPlot + labs(y="Petal length (cm)", x = "Sepal length (cm)")) + ggtitle("Petal and sepal length of iris") | |
# Center the title (which is left-aligned by default) | |
print(IrisPlot + labs(y="Petal length (cm)", x = "Sepal length (cm)")) + ggtitle("Petal and sepal length of iris") + theme(plot.title = element_text(hjust = 0.5)) | |
# Change the title aesthetics | |
ggplot(pdata, aes(tm, colour=tot_readsbins)) + geom_freqpoly(binwidth = 5) + ggtitle("Tm (by depth)") + theme(plot.title = element_text(size=40, face = "bold")) | |
# Example for geom_smooth : for drawing smoothed lines (e.g., for simple trends or approximations) | |
ggplot(mpg, aes(x = displ, y = hwy)) + geom_smooth() | |
# Using 2 different geometries in the same plot | |
ggplot(mpg, aes(x = displ, y = hwy)) + geom_point() + geom_smooth() | |
# Save to pdf plot | |
library("ggplot2") | |
pdf("myplot.pdf") | |
myplot <- ggplot(mtcars, aes(wt, mpg)) + geom_point() | |
print(myplot) | |
dev.off() | |
# Save to png plot | |
library("ggplot2") | |
png("myplot.png") # changing figure size png("plot.png", width=480, height=240, res=120) | |
myplot <- ggplot(mtcars, aes(wt, mpg)) + geom_point() | |
print(myplot) | |
dev.off() | |
# to save a plot that you have opened use | |
ggsave("plot.pdf") # if you want to know the size of the plot that will be saved use dev.size() | |
### creating a multipage pdf | |
#specify path to save PDF to | |
destination = 'pdf_ex.pdf' | |
#open PDF | |
pdf(file=destination) | |
#save plots to PDF | |
for (i in 1:4) { | |
x=rnorm(i) | |
y=rnorm(i) | |
plot(x, y) | |
} | |
#turn off PDF plotting | |
dev.off() | |
# colour pallete , useful when you want to use several colours in a gradient | |
ggplot(data, aes(x, y, # colour_brewer & fill_brewer | |
col = factor(x), | |
fill = factor(x))) + | |
geom_bar(stat = "identity", size = 2) + | |
scale_colour_brewer(palette = "YlOrRd", direction = - 1) + | |
scale_fill_brewer(palette = "BuPu") | |
# if, instead of using a predefined pallete, we want to use the customised colours we need to do: | |
p <- ggplot(mtcars, aes(mpg, wt)) + | |
geom_point(aes(colour = factor(cyl))) | |
p + scale_colour_manual(values = c("red", "blue", "green")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment