#r #ggplot2
Вопрос:
Мои метки geom_points ggrepel не совпадают со вторичной осью.Он собирает номера первичной оси. Как мне это изменить?
BHAGWANPUR lt;- 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 lt;- BHAGWANPUR %gt;% mutate(mpg_type=ifelse(BHAGWANPUR$SWINGgt;0,"ABOVE","BELOW")) BHAGWANPUR$PARTY lt;- factor(BHAGWANPUR$PARTY,levels=BHAGWANPUR$PARTY) BHAGWANPUR draw lt;- 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 lt;- 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
Ответ №1:
То же самое отсутствует и с предыдущим вопросом. Использование 1000 * BASE_SHARE
было пропущено geom_label_repel
draw lt;- 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 lt;- 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