daramireh / rfordatasciencebook

0 stars 0 forks source link

First chapters ggplot2 package #1

Open daramireh opened 2 years ago

daramireh commented 2 years ago

library(tidyverse) install.packages("installr", dependencies = TRUE) library(installr) updateR()

ggplot2::mpg #dataset type of cars in EEUU

ggplot(data=mpg)+ geom_point(mapping=aes(x=displ, y = hwy))

ggplot(data=mpg)+ geom_area(mapping= aes(x=displ, y = hwy))

ggplot(data=mpg)+ geom_point(mapping=aes(x=hwy, y = cyl))

ggplot(data=mpg)+ geom_point(aes(x=class, y = drv))

ggplot(data=mpg)+ geom_point(mapping = aes(x=displ, y=hwy, color=class))

ggplot(data = mpg)+ geom_point(mapping = aes(x = displ, y = hwy))+ facet_wrap(~class, nrow=2)

ggplot(data = mpg)+ geom_point(mapping = aes(x = displ, y = hwy))+ facet_grid(drv~cyl)

ggplot(data = mpg) + geom_point(mapping = aes(x = drv, y = cyl))

left

ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy))

right

ggplot(data = mpg) + geom_smooth(mapping = aes(x = displ, y = hwy))

ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) + geom_smooth(mapping = aes(x = displ, y = hwy))

ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) + geom_smooth( data = filter(mpg, class == "subcompact"), se = FALSE )

ggplot2::diamonds #dataset type of diamonds

ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut))

ggplot(data = diamonds) + stat_count(mapping = aes(x = cut))

ggplot(data = diamonds) + geom_bar( mapping = aes(x = cut, y = ..prop.., group = 1) )

ggplot(data = diamonds) + stat_summary( mapping = aes(x = cut, y = depth), fun.ymin = min, fun.ymax = max, fun.y = median )

ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, y = ..prop..))

ggplot(data = diamonds) + geom_bar( mapping = aes(x = cut, fill = color, y = ..prop..) ) ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, color = cut))

ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = cut))

ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = clarity))

ggplot( data = diamonds, mapping = aes(x = cut, fill = clarity) ) + geom_bar(alpha = 1/5, position = "identity") ggplot( data = diamonds, mapping = aes(x = cut, color = clarity) ) + geom_bar(fill = NA, position = "identity")

position adjustment fill is for the proportion

show the completed bar acomulated proportion or distribution

ggplot(data = diamonds) + geom_bar( mapping = aes(x = cut, fill = clarity), position = "fill" )

position adjustment dodge show a paraleras bar

ggplot(data = diamonds) + geom_bar( mapping = aes(x = cut, fill = clarity), position = "dodge" )

caja y bigotes vertigal

ggplot(data = mpg, mapping = aes(x = class, y = hwy)) + geom_boxplot()

caja y bigotes horizontal coord_flip()

ggplot(data = mpg, mapping = aes(x = class, y = hwy)) + geom_boxplot() + coord_flip()

cambio de eje y rotacion para grafico de tortas

bar <- ggplot(data = diamonds) + geom_bar( mapping = aes(x = cut, fill = cut), show.legend = FALSE, width = 1 ) + theme(aspect.ratio = 1) + labs(x = NULL, y = NULL)

bar + coord_flip() bar + coord_polar() #grafico de torta

esqueleto para hacer graficos con ggplot

ggplot(data = ) +

(

mapping = aes(),

stat = ,

position =

) +

+