Skip to content

Instantly share code, notes, and snippets.

@ashutoshnanda
Created March 1, 2016 00:02
Show Gist options
  • Save ashutoshnanda/7e1d24564754e08a6fb4 to your computer and use it in GitHub Desktop.
Save ashutoshnanda/7e1d24564754e08a6fb4 to your computer and use it in GitHub Desktop.
Introduction to Plotting with ggplot2
install.packages("ggplot2")
library(ggplot2)
x <- rnorm(50)
x
ggplot() + geom_histogram(aes(x = x))
ggplot() + geom_histogram(aes(x = x), binwidth = 0.1)
x <- rnorm(500)
ggplot() + geom_histogram(aes(x = x), binwidth = 0.1)
length(x)
my.data <- rnorm(1000)
ggplot() + geom_histogram(aes(x = my.data), bin.width = 0.1)
ggplot() + geom_histogram(aes(x = my.data), binwidth = 0.1)
ggplot() + geom_histogram(aes(x = my.data), bins = 20)
ggplot() + geom_histogram(aes(x = my.data), bins = 230)
ggplot() + geom_histogram(aes(x = my.data), bins = 30)
ggplot() + geom_histogram(aes(x = my.data), bins = 60)
ggplot() + geom_histogram(aes(x = my.data), bins = 30, color = "blue")
ggplot() + geom_histogram(aes(x = my.data), bins = 30, color = "blue", fill = "blue")
ggplot() + geom_histogram(aes(x = my.data), bins = 30, fill = "blue")
ggplot() + geom_histogram(aes(x = my.data), bins = 30, fill = "blue", alpha = 0.5)
ggplot() + geom_histogram(aes(x = my.data), bins = 30, fill = "blue", alpha = 0.3)
ggplot() + geom_histogram(aes(x = my.data), bins = 30, fill = "blue", alpha = 0.3) + geom_density()
ggplot() + geom_histogram(aes(x = my.data), bins = 30, fill = "blue", alpha = 0.3) + geom_density(aes(x = my.data))
ggplot() + geom_density(aes(x = my.data))
ggplot() + geom_density(aes(x = my.data), fill = "green")
ggplot() + geom_density(aes(x = my.data), fill = "green", color = "blue")
ggplot() + geom_density(aes(x = my.data), fill = "green", color = "blue") + ggtitle("My Plot")
p <- ggplot() + geom_density(aes(x = my.data), fill = "green", color = "blue") + ggtitle("My Plot")
p
p + ylab("My Label")
p
p + coord_cartesian(xlim = c(2,3))
p + coord_cartesian(xlim = c(2,3)) + ggtitle("My Zoomed In Plot")
p
p + coord_cartesian(xlim = c(2,3)) + ggtitle("My Zoomed In Plot") + xlab("Data") + ylab("Density")
ggplot() + geom_density(aes(x = my.data), fill = "green", color = "blue") + ggtitle("My Plot")
ggplot() + geom_histogram(aes(x = my.data), fill = "green", color = "blue") + ggtitle("My Plot")
ggplot() + geom_violin(aes(x = my.data), fill = "green", color = "blue") + ggtitle("My Plot")
x <- seq(-10, 10, 0.1)
x
y <- x * 3 + x^2 + sin(x) - cos(x) + rnorm(length(x), sd = 3)
ggplot() + geom_point(aes(x = x, y = y))
x <- seq(-3, 3, 0.1)
y <- x * 3 + x^2 + sin(x) - cos(x) + rnorm(length(x), sd = 3)
x <- seq(-3, 3, 0.1)
y <- x * 3 + x^2 + sin(x) - cos(x) + rnorm(length(x), sd = 3)
ggplot() + geom_point(aes(x = x, y = y))
ggplot() + geom_point(aes(x = x, y = y)) + geom_smooth(aes(x = x, y = y))
ggplot() + geom_point(aes(x = x, y = y)) + geom_smooth(aes(x = x, y = y), method = lm)
ggplot() + geom_point(aes(x = x, y = y)) + geom_smooth(aes(x = x, y = y), method = loess)
sleep <- rnorm(100, mean = 6, sd = 1)
grade <- sleep + sleep ^ 2 / 10 + rnorm(100, sd = 5)
df <- data.frame(sleep, grade)
df
ggplot(data = df, aes(x = sleep, y = grade)) + geom_point()
grade <- sleep + sleep ^ 2 / 10 + rnorm(100, sd = 1)
df <- data.frame(sleep, grade)
ggplot(data = df, aes(x = sleep, y = grade)) + geom_point()
ggplot(data = df, aes(x = sleep, y = grade)) + geom_point() + geom_smooth()
?seq
?rep
rep.int(c(0, 1), c(50, 50))
rep <- rep.int(c(0, 1), c(50, 50))
class <- rep.int(c(0, 1), c(50, 50))
x <- runif(100, min = 10, max = 20)
x
y <- 5 * x + 40 + rnorm(100) + class * -2 * x
class <- rep.int(c(10, -10), c(2, 3))
class
class <- rep.int(c(0, 1), c(50, 50))
df <- data.frame(df, aes(x = x, y = y))
df <- data.frame(input = x, output = y, condition = class)
df
ggplot(df, aes(x = input, y = output))
ggplot(df, aes(x = input, y = output)) + geom_point()
ggplot(df, aes(x = input, y = output)) + geom_point() + facet_grid(. ~ condition)
ggplot(df, aes(x = input, y = output)) + geom_point() + facet_grid(. ~ condition) + geom_smooth()
ggplot(df, aes(x = input, y = output)) + geom_point() + geom_smooth()
ggplot(df, aes(x = input, y = output)) + geom_point() + facet_grid(. ~ condition) + geom_smooth()
y <- 5 * x + 40 + rnorm(100, sd = 10) + class * -2 * x
df <- data.frame(input = x, output = y, condition = class)
ggplot(df, aes(x = input, y = output)) + geom_point() + geom_smooth()
ggplot(df, aes(x = input, y = output)) + geom_point() + facet_grid(. ~ condition) + geom_smooth()
ggplot(df, aes(x = input, y = output)) + geom_point() + geom_smooth() + facet_grid(. ~ condition)
ggplot(df, aes(x = input, y = output)) + geom_point() + geom_smooth()
ggplot(df, aes(x = input, y = output)) + geom_point() + geom_smooth() + facet_grid(. ~ condition)
x <- runif(100, min = -10, max = 10)
y <- 5 * x + 40 + rnorm(100, sd = 4) + class * -2 * x
df <- data.frame(input = x, output = y, condition = class)
ggplot(df, aes(x = input, y = output)) + geom_point() + geom_smooth()
ggplot(df, aes(x = input, y = output)) + geom_point() + geom_smooth() + facet_grid(. ~ condition)
y <- 5 * x + 40 + rnorm(100, sd = 4) + class * -4 * x
ggplot(df, aes(x = input, y = output)) + geom_point() + geom_smooth()
df <- data.frame(input = x, output = y, condition = class)
ggplot(df, aes(x = input, y = output)) + geom_point() + geom_smooth()
ggplot(df, aes(x = input, y = output)) + geom_point() + geom_smooth() + facet_grid(. ~ condition)
ggplot(df, aes(x = input, y = output)) + geom_point() + geom_smooth() + facet_grid(. ~ condition) + theme_bw()
ggplot(df, aes(x = input, y = output)) + geom_point() + geom_smooth() + facet_grid(. ~ condition) + theme_classic()
ggplot(df, aes(x = input, y = output)) + geom_point() + geom_smooth() + facet_grid(. ~ condition) + theme_minimal()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment