library(dslabs)
library(dplyr)
##
## Adjuntando el paquete: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
data("murders")
ggplot(data = murders)
ggplot(data=murders,aes(x = population/10^6, y = total))+
geom_point()
murders %>% ggplot(aes(x = population/10^6, y = total)) +
geom_point()
murders %>% ggplot() +
geom_point(aes(x = population/10^6, y = total))
murders %>% ggplot(aes(x = population/10^6,
y = total,color=region,shape=region)) +
geom_point(show.legend = FALSE)+xlab("Populations in millions (log scale)") +
ylab("Total number of murders (log scale)") +
ggtitle("US Gun Murders in 2010")+
scale_x_continuous(trans = "log10") +
scale_y_continuous(trans = "log10") +
facet_wrap(~ region, nrow = 2)
murders %>% ggplot(aes(x = population/10^6,
y = total,color=region,shape=region)) +
geom_point(show.legend = FALSE)+xlab("Populations in millions (log scale)") +
ylab("Total number of murders (log scale)") +
ggtitle("US Gun Murders in 2010")+
scale_x_continuous(trans = "log10") +
scale_y_continuous(trans = "log10") +
facet_wrap(~ region, nrow = 2)+
geom_smooth(show.legend = FALSE)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
p<- murders %>% ggplot(aes(x = population/10^6,
y = total,color=region)) +
geom_point(aes(shape=region))+xlab("Populations in millions (log scale)") +
ylab("Total number of murders (log scale)") +
ggtitle("US Gun Murders in 2010")+
scale_x_continuous(trans = "log10") +
scale_y_continuous(trans = "log10")
p
#install.packages("ggthemes")
library(ggthemes)
p + theme_economist()
#Diagrama de barras
murders %>%
count(region) %>%
mutate(porcentaje = (n/sum(n))*100) %>%
ggplot(aes(region, porcentaje,fill = region)) +
geom_bar(stat = "identity") +
geom_text(aes(label = sprintf(
"%.1f%%", porcentaje)),position = position_stack(vjust = 0.8))
#Diagrama de barras ordenado de mayor a menor
murders %>%
count(region) %>%
mutate(proportion = n/sum(n)) %>%
ggplot(aes(x=reorder(region, -n), proportion,fill = region)) +
geom_bar(stat = "identity")
#Diagrama de barras ordenado de menor a mayor
murders %>%
count(region) %>%
mutate(proportion = n/sum(n)) %>%
ggplot(aes(x=reorder(region, n), proportion,fill = region)) +
geom_bar(stat = "identity")
#Diagrama circular
murders %>%
count(region) %>%
mutate(proportion = n/sum(n)) %>%
ggplot(aes(x="", proportion,fill = region)) +
geom_bar(stat = "identity",width = 1) +
coord_polar(theta = "y") +
geom_text(aes(label = paste0(
scales::percent(proportion))),position = position_stack(vjust = 0.5))
#Boxplot
murders <- murders %>% mutate(murders,rate= total/population*100000)
murders %>%
select(region,rate) %>%
filter(region=="South") %>%
ggplot(aes(region,rate,fill=region)) + geom_boxplot()
murders %>% ggplot(aes(region,rate,fill=region)) +
geom_boxplot()
# Facetas
murders %>%
count(region) %>%
mutate(proportion = n/sum(n)) %>%
ggplot(aes(region, proportion,fill = region)) +
geom_bar(stat = "identity") +
facet_wrap(~ region, nrow = 2)
#Varios gráficos
x <- log10(murders$population)
y <- murders$total
p1 <- data.frame(x = x, y = y) %>%
ggplot(aes(x,y)) +
geom_point()
p2 <- data.frame(x = x) %>%
ggplot(aes(x = x)) +
geom_histogram()
library(gridExtra)
##
## Adjuntando el paquete: 'gridExtra'
## The following object is masked from 'package:dplyr':
##
## combine
grid.arrange(p1, p2, ncol = 2)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.