Skip to content

Instantly share code, notes, and snippets.

@sillasgonzaga
Last active November 1, 2017 00:10
Show Gist options
  • Save sillasgonzaga/ae62d57836c37ebff4a5f7a8dc32eeb7 to your computer and use it in GitHub Desktop.
Save sillasgonzaga/ae62d57836c37ebff4a5f7a8dc32eeb7 to your computer and use it in GitHub Desktop.
meu_tema <- function(base.size = 9, legend.text.size = 9, axis.text.size = 10, axis.title.size = 11) {
# exemplo da função lm
#ggplot(agua_agregado, aes(x = dif_pressao, y = vazao_massica_media)) +
# geom_point() + geom_smooth(method = "lm") +
# meu_tema() +
# labs(x = "Diferença de Pressão (Pa)", y = "Vazão mássica (kg)") +
# annotate("text", x = 1500, y = 0.0035, parse = TRUE, size = 3, hjust = 0.25,
# label = lm.equation(lm(vazao_massica_media ~ dif_pressao, data = agua_agregado),
# digits = 10, digitsR2 = 4))
library(ggplot2)
library(RColorBrewer)
library(scales)
library(grid)
cinza <- brewer.pal("Greys", n=9)[6]
# Generate the colors for the chart procedurally with RColorBrewer
palette <- brewer.pal("Greys", n=9)
color.background = palette[2]
color.grid.major = palette[3]
color.axis.text = palette[6]
color.axis.title = palette[7]
color.title = palette[9]
# Begin construction of chart
theme_bw(base_size = base.size) +
# Set the entire chart region to a light gray color
theme(panel.background=element_rect(fill=color.background, color=color.background)) +
theme(plot.background=element_rect(fill=color.background, color=color.background)) +
theme(panel.border=element_rect(color=color.background)) +
# Format the grid
theme(panel.grid.major=element_line(color=color.grid.major,size=.25)) +
theme(panel.grid.minor=element_blank()) +
theme(axis.ticks=element_blank()) +
# Format the legend, but hide by default
theme(legend.position="none") +
theme(legend.background = element_rect(fill=color.background)) +
theme(legend.text = element_text(size=legend.text.size, color=color.axis.title)) +
# Set title and axis labels, and format these and tick marks
theme(plot.title=element_text(color=color.title, size=10, vjust=1.25)) +
theme(axis.text.x=element_text(size=axis.text.size, color=color.axis.text)) +
theme(axis.text.y=element_text(size=axis.text.size, color=color.axis.text)) +
theme(axis.title.x=element_text(size=axis.title.size, color=color.axis.title, vjust=0)) +
theme(axis.title.y=element_text(size=axis.title.size, color=color.axis.title, vjust=1.25)) +
# remove boxes around legend symbols
theme(legend.key = element_blank()) +
# Plot margins
theme(plot.margin = unit(c(0.35, 0.2, 0.3, 0.35), "cm"))
}
# adicionar equação de regressão linear no gráfico
lm.equation = function(x, digits = 4, digitsR2 = 3) {
lm_coef <- list(a = round(coef(x)[1], digits = digits),
b = round(coef(x)[2], digits = digits),
r2 = round(summary(x)$r.squared, digits = digitsR2));
lm_eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(R)^2~"="~r2,lm_coef)
as.character(as.expression(lm_eq));
}
# definir função adicional para forçar b = 0
lm.equation0 = function(x, digits = 4, digitsR2 = 3) {
lm_coef <- list(
a = round(coef(x)[1], digits = digits),
#b = round(coef(x)[2], digits = digits), # b forçado a 0
r2 = round(summary(x)$r.squared, digits = digitsR2)
)
lm_eq <- substitute(italic(y) == 0 + a %.% italic(x)*","~~italic(R)^2~"="~r2,lm_coef)
as.character(as.expression(lm_eq));
}
# exemplo: http://t-redactyl.io/blog/2016/05/creating-plots-in-r-using-ggplot2-part-11-linear-regression-plots.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment