February 15, 2021

R - ggplot2 package

ggplot2 package in R language

library(ggplot2)

ggplot(data = NULL, mapping = aes(), ..., environment = parent.frame()) # gg - grammar of graphics
ggplot(email50, aes(x = number_yn)) + geom_bar()
ggplot(data = hsb2, aes(x=science, y=math)) + geom_point()
ggplot(data = hsb2, aes(x=science, y=math, color=prog)) + geom_point()
mtc_plot <- ggplot(mtcars, aes(x = factor(cyl), y = mpg)) +  geom_point()
ggplot(mtcars, aes(x = wt, y = mpg, shape = disp)) + geom_point()
ggplot(mammals, aes(x=body, y=brain)) + geom_point(alpha=0.6) + stat_smooth(method="lm", col="red", se=FALSE)
ggplot(mammals, aes(x=body, y=brain)) + geom_point(alpha=0.6) + coord_fixed() + scale_x_log10() + scale_y_log10() + stat_smooth(method="lm", col="#C421243", se=FALSE, size=1)
ggplot(df, aes(gp, y)) + geom_point() + geom_point(data = ds, aes(y = mean), colour = 'red', size = 3)
ggplot() + geom_point(data = df, aes(gp, y)) + geom_point(data = ds, aes(gp, mean), colour = 'red', size = 3) + geom_errorbar( data = ds, aes(gp, mean, ymin = mean - sd, ymax = mean + sd), colour = 'red', width = 0.4 )
ggplot(mtcars, aes(wt, mpg)) + geom_point(colour = "red", size = 3)
dia_plot <- ggplot(diamonds, aes(x=carat, y=price, col=clarity)) + geom_smooth() + geom_point(alpha=0.4)
ggplot(mtcars, aes(wt, mpg)) +  geom_point(shape = 21, colour = "black", fill = "white", size = 5, stroke = 5)
ggplot(mtcars2, aes(wt, mpg)) + geom_point(na.rm = TRUE)
ggplot(mtcars, aes(x = wt, y = mpg, col = factor(cyl))) + geom_point()
dia_plot <- ggplot(diamonds, aes(carat, price)) + geom_boxplot()
ggplot(diamonds, aes(carat, price)) + geom_boxplot(aes(group = cut_width(carat, 0.25)), outlier.alpha = 0.1)
ggplot(rpart::kyphosis, aes(Age, as.numeric(Kyphosis) - 1)) + geom_jitter(height = 0.05) +  binomial_smooth()
ggplot(mtcars, aes(x = wt,y = mpg, size = disp, col= hp)) +  geom_text(aes(label = cyl))
with(dfr[(dfr$var3 < 155) & (dfr$var4 > 27),], plot(var1, var2))
with(d, plot(x, n, pch=16, axes=F, xlab=NA, ylab=NA, cex=1.2))
ggplot(mtcars, aes(x=cyl, y=wt)) + geom_point(position = "jitter")
ggplot(mtcars, aes(x = wt, y = mpg, col = factor(cyl), label = cyl)) + geom_label()

aes(x, y, ...)
aes(mpg, wt)
aes(x = mpg ^ 2, y = wt / cyl)
aes(color = x)
aes(fg = x)

layer(geom = NULL, stat = NULL, data = NULL, mapping = NULL, position = NULL, params = list(), inherit.aes = TRUE, check.aes = TRUE, check.param = TRUE, subset = NULL, show.legend = NA)
ggplot(mpg, aes(displ, hwy)) +
  layer(geom = "point", stat = "identity", position = "identity", data = head, params = list(na.rm = FALSE) )

borders(database = "world", regions = ".", fill = NA, colour = "grey50", xlim = NULL, ylim = NULL, ...)
ggplot(ia, aes(long, lat)) +
  geom_polygon(aes(group = group), fill = NA, colour = "grey60") +
  geom_text(aes(label = subregion), data = seats, size = 2, angle = 45)
ggplot(capitals, aes(long, lat)) +
  borders("state") +
  geom_point(aes(size = pop)) +
  scale_size_area() +
  coord_quickmap()
ggplot(diamonds, aes(price, colour = cut)) + geom_freqpoly()
ggplot(mtcars, aes(x=mpg, col=factor(cyl))) + geom_freqpoly(binwidth=1)
ggplot() + 
  geom_polygon(data = usa, aes(x=long, y = lat, group = group), fill = NA, color = "red") + 
  coord_fixed(1.3)

qplot(data=diamonds, x=carat, y=price, color=cut) + scale_color_brewer(palette='Accent') # quick plot
qplot(data=reddit, x=age.range)
qplot(x=dob, data=pf)
qplot(carat, price, data=diamonds, color=cut, log="xy", facets=~clarity, main="Diamonds")
qplot(data$sex, geom="histogram", binwidth = 0.25, main = "Histogram for G2", xlab = "sex", fill=I("blue"))
qplot(x = friend_count, data = pf, binwidth = 25) +
  scale_x_continuous(limits = c(0, 1000), breaks = seq(0, 1000, 50))
qplot(Temp, data=airquality, binwidth=5)
qplot(age, wage, colour=class, data=training) + geom_smooth(method="lm", formula=y~x)
qplot(cwage, age, data=training, fill=cwage, geom=c("box plot","jitter"))
qplot(num_balls) + xlab("Balls") + ylab("Count") + ggtitle("Probability Distribution")
qplot(data=data, x=failures, y=G1, geom=c("point","smooth")) + labs(list(title="Failures Vs G1", x="Failures", y="G1")) 

geom_point(mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
p + geom_point(aes(shape = factor(cyl)))
p + geom_point(aes(colour = cyl)) + scale_colour_gradient(low = "blue")
p + geom_point(colour = "black", size = 4.5) + geom_point(colour = "pink", size = 4) +  geom_point(aes(shape = factor(cyl)))

geom_jitter(mapping = NULL, data = NULL, stat = "identity", position = "jitter", ..., width = NULL, height = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
ggplot(mtcars, aes(x=cyl, y=wt)) + geom_jitter()
p + geom_jitter()
p + geom_jitter(aes(colour = class))
ggplot(mpg, aes(cyl, hwy)) + geom_jitter(width = 0.25)
ggplot(mpg, aes(cty, hwy)) + geom_jitter(width = 0.5, height = 0.5)
ggplot(mtcars, aes(x=cyl, y=wt)) + geom_jitter(position=position_jitter(width=0.5))

geom_count(mapping = NULL, data = NULL, stat = "sum", position = "identity", ..., na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
ggplot(mpg, aes(cty, hwy)) + geom_count()
ggplot(mpg, aes(cty, hwy)) + geom_count() + scale_size_area()
d + geom_count(aes(size = ..prop..))
d + geom_count(aes(size = ..prop.., group = cut)) + scale_size_area(max_size = 10)
stat_sum(mapping = NULL, data = NULL, geom = "point", position = "identity", ..., na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
ggplot(diamonds, aes(x = clarity, y = carat, col = price)) + stat_sum()

geom_bin2d(mapping = NULL, data = NULL, stat = "bin2d", position = "identity", ..., na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
d + geom_bin2d()
d + geom_bin2d(bins = 30)
d + geom_bin2d(binwidth = c(0.1, 0.1))
stat_bin_2d(mapping = NULL, data = NULL, geom = "tile", position = "identity", ..., bins = 30, binwidth = NULL, drop = TRUE, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)

geom_quantile(mapping = NULL, data = NULL, stat = "quantile", position = "identity", ..., lineend = "butt", linejoin = "round", linemitre = 1, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
m + geom_quantile(quantiles = q10)
m + geom_quantile(method = "rqss", lambda = 0.1)
m + geom_quantile(colour = "red", size = 2, alpha = 0.5)
stat_quantile(mapping = NULL, data = NULL, geom = "quantile", position = "identity", ..., quantiles = c(0.25, 0.5, 0.75), formula = NULL, method = "rq", method.args = list(), na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)

geom_bar(mapping = NULL, data = NULL, stat = "count", position = "stack", ..., width = NULL, binwidth = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
ggplot(driver_years, aes(y=driverdeaths, x=date_year)) + geom_bar(stat="identity")
ggplot(mtcars, aes(x = factor(cyl), fill =factor(am))) +
   geom_bar(alpha = 0.5, position = position_dodge())
ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) + geom_bar(position = "dodge")
ggplot(data=dat1, aes(x=time, y=total_bill, fill=sex)) +
  geom_bar(stat="identity", position=position_dodge(), colour="black") +
  scale_fill_manual(values=c("#999999", "#E69F00"))
ggplot(data=dat1, aes(x=time, y=total_bill, fill=sex)) +
  geom_bar(colour="black", stat="identity", position=position_dodge(), size=.3) +                       
  scale_fill_hue(name="Sex of payer") +     
  xlab("Time of day") + ylab("Total bill") + ggtitle("Average bill for 2 people") +    
  theme_bw()
ggplot(data=uber, aes(x=request_hour, fill=factor(request_status))) +
  geom_bar(position = "dodge", colour="black") +
  xlab("Time of day") + ylab("Total Requests") + ggtitle("Demand and Supply Gap") +  
  scale_fill_manual("Request Status", values=c("green3", "red4")) + theme_bw() +
  theme(legend.position = "top")
p =  n + scale_fill_manual(
    values = c("skyblue", "royalblue", "blue", "navy"),
    limits = c("d", "e", "p", "r"), breaks =c("d", "e", "p", "r"),
    name = "fuel", labels = c("Diesel", "Electric", "Petrol", "Hybrid"))
g +
 geom_bar(aes(fill = drv), position = position_stack(reverse = TRUE)) +
 coord_flip() +
 theme(legend.position = "top")
p <- ggplot(df, aes(x = color)) + 
  geom_bar(aes(y = ..count../sum(..count..), fill = cut)) + 
  scale_fill_brewer(palette = "Set3")
ggplot(dfl, aes(x, y=y, fill=x)) + geom_bar(stat="identity") +
    geom_text(aes(label=y), vjust=0) +
    opts(axis.text.x=theme_blank(),
        axis.ticks=theme_blank(),
        axis.title.x=theme_blank(),
        legend.title=theme_blank(),
        axis.title.y=theme_blank()
)
h + coord_flip() + scale_x_reverse()
ggplot(data=uber, aes(x=request_hour, fill=factor(request_status))) +  
  geom_bar(position = "stack", colour="black") +
  xlab("Time of day") + ylab("Total Requests") + ggtitle("Demand and Supply Gap") +  
  scale_fill_manual("Request Status", values=c("green3", "red4"), labels = c("Request Successful", "Request Unsuccessful")) + 
  coord_flip() + 
  theme(legend.position = "top") + 
  geom_text(stat='count',aes(label=abs(..count..)))
ggplot(linetypes, aes(0, y)) + 
  geom_segment(aes(xend = 5, yend = y, linetype = lty)) + 
  scale_linetype_identity() + 
  geom_text(aes(label = lty), hjust = 0, nudge_y = 0.2) +
  scale_x_continuous(NULL, breaks = NULL) + 
  scale_y_continuous(NULL, breaks = NULL)

hjust and vjust are only defined between 0 and 1:
0 means left-justified
1 means right-justified

ggplot(lc, aes(x=purpose, fill=purpose)) + geom_bar() +
  geom_text(stat = "count", aes(x=purpose, label=..count..), position=position_stack(vjust=0.5)) +
  labs(x="Loan Purpose", y="No of requests", title="Loan Purpose") +
  theme(axis.text.x=element_text(angle = 90, hjust = 0))

geom_col(mapping = NULL, data = NULL, position = "stack", ..., width = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
ggplot(sleep, aes(x = ID, y = extra, fill = group)) + geom_col()
ggplot(sleep, aes(x = ID, y = extra, fill = group)) + geom_col(position="dodge")
ggplot(USArrests, aes(x = row.names(USArrests), y = USArrests$Murder, lab)) +  geom_col()
ggplot(USArrests, aes(x = row.names(USArrests), y = USArrests$Murder, lab)) + 
  geom_col() + 
  theme(axis.text.x=element_text(angle=90, hjust=1))

stat_count(mapping = NULL, data = NULL, geom = "bar", position = "stack", ..., width = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
ggplot(df, aes(factor(var2))) +
      geom_bar(fill="dodgerblue3")+
      labs(x = NULL,y = NULL)+
      stat_count(aes(label = paste(prop.table(..count..) * 100, "%", sep = "")),
               vjust = 1, geom = "text", position = "identity", color ="white")
stat="bin"   (default)
stat="identity"
stat="count"

geom_line(mapping = NULL, data = NULL, stat = "identity", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ...)
ggplot(economics, aes(x=date, y=unemploy)) + geom_line()
ggplot(economics, aes(x=date, y=unemploy/pop, col=psavert)) + geom_line() + geom_point()
ggplot(ukacc, aes(date)) + 
  geom_line(aes(y = rear, colour = "blue"))+
  geom_line(aes(y = drivers, colour = "red3")) + 
  geom_line(aes(y = front, colour = "green"))
ggplot(ukacc, aes(x=date, y=drivers), col="black", size=1) + geom_line() +
  geom_line(data=ukacc, aes(x=date, y=front), col="red", size=1) +
  geom_line(data=ukacc, aes(x=date, y=rear), col="blue", size=1)
geom_path(mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., lineend = "butt", linejoin = "round", linemitre = 1, arrow = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
ggplot(economics[economics$date > as.Date("2010-01-01"),], aes(x=date, y=unemploy)) + geom_path()
geom_step(mapping = NULL, data = NULL, stat = "identity", position = "identity", direction = "hv", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ...)
ggplot(economics, aes(x=date, y=unemploy)) + geom_step()
ggplot(economics[economics$date > as.Date("2010-01-01"), ], aes(x=date, y=unemploy)) + geom_step()

geom_boxplot(mapping = NULL, data = NULL, stat = "boxplot", position = "dodge", ..., outlier.colour = NULL, outlier.color = NULL, outlier.fill = NULL, outlier.shape = 19, outlier.size = 1.5, outlier.stroke = 0.5, outlier.alpha = NULL, notch = FALSE, notchwidth = 0.5, varwidth = FALSE, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
stat_boxplot(mapping = NULL, data = NULL, geom = "boxplot", position = "dodge", ..., coef = 1.5, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
p + geom_boxplot()
p + geom_boxplot() + geom_jitter(width = 0.2)
p + geom_boxplot() + coord_flip()
p + geom_boxplot(notch = TRUE, varwidth = TRUE)
p + geom_boxplot(fill = "white", colour = "#3366FF")
p + geom_boxplot(outlier.colour = "red", outlier.shape = 1)
ggplot(ukacc_new, aes(group=date_year, x=date_year, y=drivers)) + geom_boxplot()
ggplot(lc, aes(x=lc$grade, y=lc$loan_amnt)) + geom_boxplot(aes(fill = grade)) +
  theme(plot.title=element_text(size=14),
        axis.text=element_text(size=14),
        axis.title=element_text(size=14)) +
  labs(list(
    title = "Loan Amount by Grade",
    x = "Grade",
    y = "Loan Amount")
  )  

p<- ggplot(data = diamonds)
p <- p + aes(x = carat, y = depth)
p <- p + geom_point()
p <- p + geom_density2d() + facet_grid(cut~.)

geom_smooth(mapping = NULL, data = NULL, stat = "smooth", position = "identity", ..., method = "auto", formula = y ~ x, se = TRUE, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
stat_smooth(mapping = NULL, data = NULL, geom = "smooth", position = "identity", ..., method = "auto", formula = y ~ x, se = TRUE, n = 80, span = 0.75, fullrange = FALSE, level = 0.95, method.args = list(), na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
ggplot(mpg, aes(displ, hwy)) + geom_point() +  geom_smooth(span = 0.3)
ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  geom_smooth(method = "lm", formula = y ~ splines::bs(x, 3), se = FALSE)
ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  geom_smooth(span = 0.8) +
  facet_wrap(~drv)
ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  facet_wrap(c("cyl", "drv"), labeller = "label_both")
ggplot(economics_long, aes(date, value)) +
  geom_line() +
  facet_wrap(~variable, scales = "free_y", nrow = 2, strip.position = "bottom") +
  theme(strip.background = element_blank(), strip.placement = "outside")
ggplot(diamonds, aes(x=price)) + geom_histogram(binwidth=100) + facet_wrap(~cut)

geom_violin(mapping = NULL, data = NULL, stat = "ydensity", position = "dodge", ..., draw_quantiles = NULL, trim = TRUE, scale = "area", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
stat_ydensity(mapping = NULL, data = NULL, geom = "violin", position = "dodge", ..., bw = "nrd0", adjust = 1, kernel = "gaussian", trim = TRUE, scale = "area", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
p + geom_violin()
p + geom_violin() + geom_jitter(height = 0, width = 0.1)
p + geom_violin(scale = "count")
p + geom_violin(trim = FALSE)
p + geom_violin(aes(fill = factor(cyl)))
p + geom_violin(fill = "grey80", colour = "#3366FF")
p + geom_violin(draw_quantiles = c(0.25, 0.5, 0.75))
m + geom_violin() + scale_y_log10() + coord_trans(y = "log10")

p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)
p1 + theme(axis.text = element_text(colour = "blue"))
p1 + theme(axis.ticks.length = unit(.25, "cm"))
p2 + theme(legend.justification = "top")
p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))
p2 + theme(
  legend.position = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)
bar_theme <- theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5), legend.position="none")
box_theme <- theme(axis.line.y=element_blank(),axis.title.y=element_blank(),  axis.ticks.y=element_blank(), axis.text.y=element_blank(), legend.position="none")

scale_x_discrete() 
scale_x_continuous()
ggplot(mtcars, aes(x=mpg)) + geom_histogram()
ggplot(mtcars, aes(x=mpg, fill=factor(cyl))) + geom_histogram(binwidth=1)
ggplot(aes(x = dob_day), data = pf) + geom_histogram(binwidth = 2) +  scale_x_continuous(breaks = 1:31)
ggplot(aes(x = friend_count), data = subset(pf, !is.na(gender))) +
  geom_histogram() +
  scale_x_continuous(limits = c(0, 1000), breaks = seq(0, 1000, 50)) +
  facet_wrap(~gender)
ggplot(aes(x = tenure / 365), data = pf) +
  geom_histogram(color = 'black', fill = '#F79420') +
  scale_x_continuous(breaks = seq(1, 7, 1), limits = c(0, 7)) +
  xlab('Number of years using Facebook') +
  ylab('Number of users in sample')
ggplot(diamonds, aes(price, fill = cut)) + geom_histogram(position="dodge")
ggplot(grades, aes(day)) + geom_histogram(stat = "count")

ggplot(data = melted_cormat, aes(x=Var1, y=Var2, fill=value)) + geom_tile()
ggplot(z.m, aes(X1, X2, fill = value)) + geom_tile() + scale_fill_gradient(low = "blue",  high = "yellow")
ggplot(data = melted_cormat, aes(Var2, Var1, fill = value))+
 geom_tile(color = "white")+
 scale_fill_gradient2(low = "blue", high = "red", mid = "white", 
   midpoint = 0, limit = c(-1,1), space = "Lab", 
   name="Pearson\nCorrelation") +
  theme_minimal()+ 
 theme(axis.text.x = element_text(angle = 45, vjust = 1, 
    size = 12, hjust = 1))+
 coord_fixed()

p + geom_linerange(aes(ymin = y - 1, ymax = y + 1), position = "dodge")
p + geom_errorbar( aes(ymin = y - 1, ymax = y + 1), width = 0.2, position = "dodge")

geom_abline(mapping = NULL, data = NULL, ..., slope, intercept, na.rm = FALSE, show.legend = NA)

geom_hline(mapping = NULL, data = NULL, ..., yintercept, na.rm = FALSE, show.legend = NA)
ggplot(media, aes(day, error)) + geom_point() + 
 scale_x_continuous(name = "days", breaks = seq(0,90,10), limits = c(0,84)) + 
 scale_y_continuous(name = "Error", breaks = seq(-250000,250000,50000), limits = c(-250000,250000)) +
 geom_hline(yintercept = 0)

geom_vline(mapping = NULL, data = NULL, ..., xintercept, na.rm = FALSE, show.legend = NA)
plot_1 + geom_vline(xintercept = as.numeric((ukacc$date)), linetype=4, size = 4, alpha = 0.5)
ggplot(data=mydata,aes(y=somevalues,x=datefield,color=category)) +
      layer(geom="line") + geom_vline(xintercept=mydata$datefield[120],linetype=4)

ggplot(s, aes(x=factor(x1), y=s, width=0.18)) + geom_bar(stat="identity", fill="#f98866", width=0.25) +
    theme_minimal() +
    coord_cartesian(ylim = c(0, 40)) + 
xlab("X (Number of red balls)") + ylab("Frequency") + theme(legend.position = "none") + theme(text = element_text(size=14))

d=expand.grid(h=seq(0,350,10), c=seq(0,100,5), l=seq(0,100,20))
ggplot() +
coord_polar(theta="x")+facet_wrap(~l) +
scale_x_continuous(name="hue", limits=c(0,360), breaks=seq(5,345,20), labels=seq(0,340,20)) +
scale_y_continuous(name="chroma", breaks=seq(0, 100, 20)) +
scale_fill_identity() +
geom_rect(data=d, mapping=aes(xmin=h, xmax=h+resolution(h), ymin=c, ymax=c+resolution(c), fill=hcl(h,c,l)), color="white", size=0.1)

ggplot() +
facet_wrap(~b) +
scale_x_continuous(name="red", breaks=seq(0.05, 1.05, 0.2), labels=seq(0, 1, 0.2)) +
scale_y_continuous(name="green", breaks=seq(0.05, 1.05, 0.2), labels=seq(0, 1, 0.2)) +
scale_fill_identity() +
geom_rect(data=d, mapping=aes(xmin=r, xmax=r+resolution(r), ymin=g, ymax=g+resolution(g), fill=rgb(r,g,b)), color="white", size=0.1)

geom_map(mapping = NULL, data = NULL, stat = "identity", ..., map, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
ggplot(values) + geom_map(aes(map_id = id), map = positions) + expand_limits(positions)
ggplot(values, aes(fill = value)) + geom_map(aes(map_id = id), map = positions) + expand_limits(positions) + ylim(0, 3)

usa <- map_data("usa")
w2hr <- map_data("world2Hires")
states <- map_data("state")
ggplot(data = states) + 
  geom_polygon(aes(x = long, y = lat, fill = region, group = group), color = "white") + 
  coord_fixed(1.3) +
  guides(fill=FALSE)


Related aRticles:  R shiny package  ggthemes ggraph lattice ggmap Packages


No comments:

Post a Comment