gpt4 book ai didi

r - 根据投票百分比同时移动所有几何条

转载 作者:行者123 更新时间:2023-12-04 17:10:31 26 4
gpt4 key购买 nike

我无法移动所有的条。我如何实现它?所有条形图应从 0 开始并移动到所需的摆动百分比。 enter image description here

library(ggplot2)
library(dplyr)
library(gganimate)
theme_set(theme_bw())
#data <- read.csv("C:\\Daman\\swig.csv")
BHAGWANPUR <- structure(list(STATE = c("UTTARAKHAND", "UTTARAKHAND", "UTTARAKHAND"
), DISTRICT = c("HARDWAR", "HARDWAR", "HARDWAR"), AC = c("BHAGWANPUR",
"BHAGWANPUR", "BHAGWANPUR"), PARTY = structure(1:3, .Label = c("INC",
"BJP", "BSP"), class = "factor"), 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), mpg_type = c("ABOVE",
"ABOVE", "BELOW")), row.names = c(NA, -3L), class = "data.frame")
BHAGWANPUR$PARTY <- factor(BHAGWANPUR$PARTY,levels=BHAGWANPUR$PARTY)

# Diverging Barcharts
draw <- ggplot(BHAGWANPUR, aes(x=PARTY, y=SWING, 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") +
coord_flip()+ 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"))+
transition_states(SWING, transition_length = 1, state_length = 1,wrap = TRUE) +
enter_fade() +
exit_shrink() +
ease_aes('sine-in-out')

animate(draw, renderer = gifski_renderer())


最佳答案

以下是您可以同时移动所有条形的方法:

首先,添加一个包含状态 0 和 1 的新列“state”,0 是状态,条形开始的位置(在 SWING == 0):

BHAGWANPUR <- BHAGWANPUR %>%
mutate(SWING = 0) %>%
bind_rows(BHAGWANPUR) %>%
mutate(state = case_when(SWING == 0 ~ 0,
TRUE ~ 1))

然后,将状态添加到transition_states,e。 G。像这样:

draw <- ggplot(BHAGWANPUR, aes(x=PARTY, y=SWING, label=SWING)) + 
geom_bar(stat='identity', aes(fill=PARTY)) +
coord_flip()+
transition_states(state, wrap=FALSE) +
enter_grow() +
ease_aes('sine-in-out')

现在所有的条都从零开始并同时增长到 SWING 值。我删除了 exit_shrink 并设置了 wrap = FALSE,因此条形图不会移回零,但动画只是从头开始。

关于r - 根据投票百分比同时移动所有几何条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69550459/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com