heart <- read.csv("~/Desktop/Data/heart.csv")
Bar plot of counts
#simple bar plot
library(ggplot2)
ggplot(heart, aes(x=factor(ChestPainType),fill=ChestPainType))+
geom_bar(stat="count", width=0.7)+
geom_text(stat='count', aes(label=..count..), vjust=1)+
labs(title = "bar plot",x="Chest pain type", y="Count")+theme_bw()
#change legend
ggplot(heart, aes(x=factor(ChestPainType),fill=ChestPainType))+
geom_bar(stat="count", width=0.7)+
geom_text(stat='count', aes(label=..count..), vjust=1)+
scale_fill_discrete(name = "Chest pain type",
labels=c("Asymptomatic","Atypical Angina",
"Non-Anginal Pain","Typical Angina"))+
labs(title = "bar plot", subtitle =" ",caption = '',
x="Chest pain type", y="Count")+theme_bw()
#change colors
ggplot(heart, aes(x=factor(ChestPainType),fill=ChestPainType))+
geom_bar(stat="count", width=0.7)+
scale_fill_manual(values = c('#CCCCFF', '#99CCFF', '#CCFFFF',"#FFCCCC")) +
geom_text(stat='count', aes(label=..count..), vjust=1)+
labs(title = "bar plot", subtitle =" ",caption = '',
x="Chest pain type", y="Count")+theme_bw()
#Stacked bar plot
ggplot(heart, aes(x = ChestPainType, fill = Sex)) +
geom_bar()+
geom_text(stat='count', aes(label=..count..), vjust=0.1,size=3.5)+
labs(title = "bar plot", subtitle =" ",caption = '',
x="Chest pain type", y="Count")+theme_bw()
#change colors
ggplot(heart, aes(x = ChestPainType, fill = Sex)) +
geom_bar()+
scale_fill_manual(values = c('#CCCCFF',"#FFCCCC"))+
geom_text(stat='count', aes(label=..count..), vjust=0.1,size=3.5)+
labs(title = "bar plot", subtitle =" ",caption = '',
x="Chest pain type", y="Count")+theme_bw()
#Grouped bar plot
ggplot(heart, aes(x = ChestPainType, fill = Sex)) +
geom_bar( position = 'dodge')+
geom_text(stat='count', aes(label=..count..), vjust=0.5,
position = position_dodge(0.9), size=3.5)+
labs(title = "bar plot", subtitle =" ",caption = '',
x="Chest pain type", y="Count")+theme_bw()
#change colors
ggplot(heart, aes(x = ChestPainType, fill = Sex)) +
geom_bar( position = 'dodge')+
scale_fill_manual(values =c('#CCCCFF',"#FFCCCC"))+
geom_text(stat='count', aes(label=..count..), vjust=0.5,
position = position_dodge(0.9), size=3.5)+
labs(title = "bar plot", subtitle =" ",caption = '',
x="Chest pain type", y="Count")+theme_bw()
bar plot by groups
#simple bar plot
ToothGrowth$dose<-as.factor(ToothGrowth$dose)
ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
geom_bar(stat="identity")+theme_bw()
#change colors
ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
geom_bar(stat="identity")+
scale_fill_manual(values = c('#CCCCFF',"#FFCCCC", '#CCFFFF'))+theme_bw()
Bar plot with multiple groups
#Stacked bar plot
ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
geom_bar(stat="identity")+theme_bw()
#change colors
ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
scale_fill_manual(values = c('#CCCCFF',"#FFCCCC"))+
geom_bar(stat="identity")+theme_bw()
#Grouped bar plot
ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
geom_bar(stat="identity", position=position_dodge())+theme_bw()
#change colors
ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
scale_fill_manual(values = c('#CCCCFF',"#FFCCCC"))+
geom_bar(stat="identity", position=position_dodge())+theme_bw()