I am trying to plot the base share % on the secondary y axis. Can anyone point out my mistake?
BHAGWANPUR <- structure(list(STATE = c("UTTARAKHAND", "UTTARAKHAND", "UTTARAKHAND"
), DISTRICT = c("HARDWAR", "HARDWAR", "HARDWAR"), AC = c("BHAGWANPUR",
"BHAGWANPUR", "BHAGWANPUR"), PARTY = c("INC", "BJP", "BSP"),
VOTES = c(44882L, 42369L, 4069L), DELTA_VOTES = c(14835L,
31719L, -32759L), VOTE_PERCENTAGE = c(48.2, 45.5, 4.4), SWING = c(9.9,
31.9, -42.5), X.CHANGE = c(49.4, 297.8, -89), BASE_SHARE = c(38.2,
13.6, 4.4)), class = "data.frame", row.names = c(NA, -3L))
BHAGWANPUR <- BHAGWANPUR %>% mutate(mpg_type=ifelse(BHAGWANPUR$SWING>0,"ABOVE","BELOW"))
BHAGWANPUR$PARTY <- factor(BHAGWANPUR$PARTY,levels=BHAGWANPUR$PARTY)
BHAGWANPUR
# Diverging Barcharts
draw <- ggplot(BHAGWANPUR, aes(x=PARTY, y=DELTA_VOTES, label=SWING))
geom_bar(stat='identity', width=.5,aes(fill=PARTY)) geom_text(aes(label=SWING,vjust="center"),position = position_stack(vjust = 0.5))
scale_fill_manual(values=c("#00BFFF","#F98C1F","#22409A")) labs(subtitle="Vote swing among parties 2012 to 2017",
title= "Bhagwanpur,Uttarakhand") theme(axis.title.x=element_blank(),axis.title.y=element_blank())
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))
draw <- draw geom_point(data = BHAGWANPUR, aes(x=PARTY, y=BASE_SHARE,group = 1))
geom_line(data = BHAGWANPUR, aes(x=PARTY, y=BASE_SHARE,group = 1), stat="identity")
scale_y_continuous(name = "Primary axis", sec.axis = sec_axis(~./2000, name = "Secondary"))
draw
CodePudding user response:
You should use BASE_SHARE * 2000
draw <- ggplot(BHAGWANPUR, aes(x=PARTY, y=DELTA_VOTES, label=SWING))
geom_bar(stat='identity', width=.5,aes(fill=PARTY)) geom_text(aes(label=SWING,vjust="center"),position = position_stack(vjust = 0.5))
scale_fill_manual(values=c("#00BFFF","#F98C1F","#22409A")) labs(subtitle="Vote swing among parties 2012 to 2017",
title= "Bhagwanpur,Uttarakhand") theme(axis.title.x=element_blank(),axis.title.y=element_blank())
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))
draw <- draw geom_point(data = BHAGWANPUR, aes(x=PARTY, y=BASE_SHARE * 2000,group = 1))
geom_line(data = BHAGWANPUR, aes(x=PARTY, y=BASE_SHARE * 2000,group = 1), stat="identity")
scale_y_continuous(name = "Primary axis", sec.axis = sec_axis(~./2000, name = "Secondary"))
draw