#ggplot2 #rprogramming #datavisualization #apa #plot
In this video i have demonstrated how to define your won theme using theme elements you defined for your best customized plot.
Its really easy. you just have to specify a function with theme name you want and in curly bracket of function just cut paste the theme elements from your best customized plot.
Now just as any predefined theme add this theme to your customized plot.
#code
library(ggplot2)
str(ToothGrowth)
ToothGrowth$dose = factor(ToothGrowth$dose)
apa theme
theme_apa = function(){
theme_classic()+
theme(
legend.background = element_rect(color="black"),
plot.title=element_text(family = "serif",size=14,face="bold"),
plot.subtitle=element_text(family = "serif",size=14, face="italic"),
axis.title = element_text(family = "serif",size=12, face="bold"),
plot.caption =element_text(hjust=0))
}
ggplot(ToothGrowth,aes(dose, len, fill=supp))+
geom_bar(stat="summary", fun="mean", position="dodge", width=0.6, color="gray60")+
stat_summary(geom="errorbar", fun.data=mean_se,position = position_dodge(width=0.6), width=0.2)+
scale_y_continuous(expand=c(0,0))+
scale_fill_manual(values=c("yellow","skyblue"))+
labs(title="Figure 1
", subtitle=" Effect of Vitamin C source on length of odontoblasts
",
caption="Note:The response is the length of odontoblasts (cells responsible for
tooth growth) in 60 guinea pigs. Each animal received one of three dose
levels of vitamin C (0.5, 1, and 2 mg/day)
by one of two delivery methods, orange juice or ascorbic
acid (a form of vitamin C and coded as VC). ", x="Dose", y="Length of Odontoblasts", fill="")+
theme_apa()
ggplot(ToothGrowth,aes(dose,len, fill=supp))+
geom_boxplot()+
labs(title="Figure 1
", subtitle=" Effect of Vitamin C source on length of odontoblasts
",
caption="Note:The response is the length of odontoblasts (cells responsible for
tooth growth) in 60 guinea pigs. Each animal received one of three dose
levels of vitamin C (0.5, 1, and 2 mg/day)
by one of two delivery methods, orange juice or ascorbic
acid (a form of vitamin C and coded as VC). ", x="Dose", y="Length of Odontoblasts", fill="")+
theme_apa()+
theme(legend.position=c(0.2, 0.85))
ggplot(iris,aes(Sepal.Length, fill=Species))+
geom_density(alpha=0.3)+
theme_apa()+
theme(legend.position=c(0.7, 0.85))
Facebook page:
/ rajendrachoureisc
Mail Id:
rajuchoure@gmail.com
youtube playlist:
• R programming tutorials
13 окт 2024