library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.3.3
## Loading tidyverse: ggplot2
## Loading tidyverse: tibble
## Loading tidyverse: tidyr
## Loading tidyverse: readr
## Loading tidyverse: purrr
## Loading tidyverse: dplyr
## Warning: package 'ggplot2' was built under R version 3.3.3
## Warning: package 'tibble' was built under R version 3.3.3
## Warning: package 'tidyr' was built under R version 3.3.3
## Warning: package 'readr' was built under R version 3.3.3
## Warning: package 'purrr' was built under R version 3.3.3
## Warning: package 'dplyr' was built under R version 3.3.3
## Conflicts with tidy packages ----------------------------------------------
## filter(): dplyr, stats
## lag(): dplyr, stats
p <- ggplot(diamonds,aes(x=cut,fill=cut)) +geom_bar()
#Rename legend, but this only works with bar (fill for bar)
p + scale_fill_discrete(name="Cut Type") + theme_bw() + ggtitle("Barplot")
Boxplot
p1<-ggplot(data=mpg,aes(x=manufacturer,y=hwy,fill=class))
# Add geom_boxplot and rename legend
p1<-p1 + geom_boxplot() + scale_fill_discrete(name="Class")
# Change angle of xlab
p1<-p1 + theme(axis.text.x = element_text(angle=30))
# Change to horizontal
p1<-p1 + coord_flip() +theme_bw()
# One may want to add jitter to see points
p1<-p1 + geom_jitter(aes(color=class),alpha=0.5,show.legend = F)
# legend.position can be changed
p1 + theme(legend.position = "bottom")
# Change numeric variable to factor in color() argument
p2<-ggplot(data=mtcars,aes(disp,y=hp,color=factor(gear,labels=c("Gear1","Gear2","Gear3"))))
p2<-p2 + geom_point()
p2
# rename legend
p2 + labs(colour="Gear") # we can rename shape="Something" if shape applied
# Add title
p2 <-p2+ xlab("Disp") + ylab("HP")
# Change text
p2 +theme(axis.text.x = element_text(size=2), plot.title = element_text(size=15,face="bold")) + labs(title="Scatter plot\n")
data_summary<-iris %>% dplyr::group_by(Species) %>% summarise(Min=min(Sepal.Length),Max=max(Sepal.Length),Mean=mean(Sepal.Length),SD=sd(Sepal.Length),n=n(),Missing_value=sum(is.na(Sepal.Length)), Q1=quantile(Sepal.Length,probs=0.25),Q3=quantile(Sepal.Length,probs=0.75),Median=median(Sepal.Length))
data_summary
## # A tibble: 3 × 10
## Species Min Max Mean SD n Missing_value Q1 Q3
## <fctr> <dbl> <dbl> <dbl> <dbl> <int> <int> <dbl> <dbl>
## 1 setosa 4.3 5.8 5.006 0.3524897 50 0 4.800 5.2
## 2 versicolor 4.9 7.0 5.936 0.5161711 50 0 5.600 6.3
## 3 virginica 4.9 7.9 6.588 0.6358796 50 0 6.225 6.9
## # ... with 1 more variables: Median <dbl>
Okay done!