My geom_points ggrepel labels not aligning with the secondary axis.It is picking up numbers of the primary axis. How do I change it?
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
draw <- ggplot(BHAGWANPUR, aes(x=PARTY, y=DELTA_VOTES, label=DELTA_VOTES))
geom_bar(stat='identity', width=.5,aes(fill=PARTY)) geom_text(aes(label=DELTA_VOTES,vjust="top"),position = position_stack(vjust = 0.5))
scale_fill_manual(values=c("#00BFFF","#F98C1F","#22409A")) labs(subtitle="2012 to 2017",
title= "Bhagwanpur,Uttarakhand")
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(colour = c("#00BFFF","#F98C1F","#22409A"),data = BHAGWANPUR, aes(x=PARTY, y=(BASE_SHARE * 1000),group = 1)) geom_label_repel(
aes(PARTY, BASE_SHARE, label = BASE_SHARE),
fontface = 'bold', color = 'black',
box.padding = unit(0.35, "lines"),
point.padding = unit(0.5, "lines"),
segment.color = 'grey50'
)
geom_line(data = BHAGWANPUR, aes(x=PARTY, y=BASE_SHARE * 1000,group = 1), stat="identity")
scale_y_continuous(name = "Votes", sec.axis = sec_axis(~./2000, name = "Base Share",labels = function(x) paste0(x, "%")))
draw
CodePudding user response:
Same missing with previous question. Use 1000 * BASE_SHARE
was missed on geom_label_repel
draw <- ggplot(BHAGWANPUR, aes(x=PARTY, y=DELTA_VOTES, label=DELTA_VOTES))
geom_bar(stat='identity', width=.5,aes(fill=PARTY)) geom_text(aes(label=DELTA_VOTES,vjust="top"),position = position_stack(vjust = 0.5))
scale_fill_manual(values=c("#00BFFF","#F98C1F","#22409A")) labs(subtitle="2012 to 2017",
title= "Bhagwanpur,Uttarakhand")
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(colour = c("#00BFFF","#F98C1F","#22409A"),data = BHAGWANPUR, aes(x=PARTY, y=(BASE_SHARE * 1000),group = 1)) geom_label_repel(
aes(PARTY, BASE_SHARE * 1000, label = BASE_SHARE),
fontface = 'bold', color = 'black',
box.padding = unit(0.35, "lines"),
point.padding = unit(0.5, "lines"),
segment.color = 'grey50'
)
geom_line(data = BHAGWANPUR, aes(x=PARTY, y=BASE_SHARE * 1000,group = 1), stat="identity")
scale_y_continuous(name = "Votes", sec.axis = sec_axis(~./2000, name = "Base Share",labels = function(x) paste0(x, "%")))
draw