I a trying to replicate what can be easily done in an excel. Using ggplot, I tried to plot the following:
- Plot a barchart, where the left Y axis is represented in counts (0-600)
- plot a line graph where the right Y axis is represented in % (0-100).
qn1 . Can someone explain to me, how can I link my percentage data to my secondary axis? Currently the line graph (which should represent the %) is plotted based on the primary Y axis using the counts scale.
qn2. How can i change the 2 scales independently?
qn3. How can i name the 2 scales independently?
ggplot() +
geom_bar(data=data,aes(x=sch,y=count,fill=category),stat = "identity")+
scale_fill_manual(values=c("darkcyan", "indianred1")) +
geom_line(data=data_percentage, aes(x=sch, y=count, group=1)) +
geom_point(data=data_percentage, aes(x=sch, y=count, group=1)) +
geom_text(data=data_percentage,aes(x=scht,label=paste(count,"%",sep="")),size=3) +
scale_y_continuous(sec.axis = sec_axis(~./2), name="%")+
theme(panel.background = element_blank(),
axis.line = element_line(colour = "black", size = 0.5, linetype = "solid"),
plot.title = element_text(size=11, face="bold", hjust=0.3),
legend.position = "top", legend.text = element_text(size=9)) +
labs(fill="") + guides(fill = guide_legend(reverse=TRUE))+
ylab("No. Recruited") + ggtitle("2. No. of students")