[英]to make a Circular stacked barplot
I want to make a circular stacked bar plot where the group is YEAR
, the value of each observation is count
.我想做一个圆形堆叠条 plot ,其中组是
YEAR
,每个观察的值是count
。
The observations are the ORIGEN
and FROM
columns.观察值是
ORIGEN
和FROM
列。 So, each ORIGEN
must contain each " FROM
" observations, f.example , samples from HOSPITAL
could be from: ER
, a specific Unit or another place.因此,每个
ORIGEN
必须包含每个“ FROM
”观察结果,例如,来自HOSPITAL
的样本可能来自: ER
、特定单位或其他地方。 I want that each barplot for Hospital
(or other ORIGEN
) is shown with different colours according " FROM
" (emergency room,exitus, so on).我希望
Hospital
(或其他ORIGEN
)的每个条形图根据“ FROM
”(急诊室、出口等)以不同的颜色显示。
I attempted next and obviously I failed!我接下来尝试了,显然我失败了!
data <- isolate_from.ORIGEN %>% gather(key = "observation", value="value", -c(1,2))
#isolate_from.ORIGEN
# Set a number of 'empty bar' to add at the end of each group
empty_bar <- 2
nObsType <- nlevels(as.factor(data$observation))
to_add <- data.frame( matrix(NA, empty_bar*nlevels(data$YEAR)*nObsType, ncol(data)) )
colnames(to_add) <- colnames(data)
to_add$YEAR <- rep(levels(data$YEAR), each=empty_bar*nObsType )
data <- rbind(data, to_add)
data <- data %>% arrange(YEAR, FROM)
data$id <- rep( seq(1, nrow(data)/nObsType) , each=nObsType)
# Get the name and the y position of each label
label_data <- data %>% group_by(id, observation) %>% summarize(tot=sum(count))
number_of_bar <- nrow(label_data)
angle <- 90 - 360 * (label_data$id-0.5) /number_of_bar # I substract 0.5 because the letter must have the angle of the center of the bars. Not extreme right(1
) or extreme left (0)
label_data$hjust <- ifelse( angle < -90, 1, 0)
label_data$angle <- ifelse(angle < -90, angle+180, angle)
# prepare a data frame for base lines
base_data <- data %>%
group_by(YEAR) %>%
summarize(start=min(id), end=max(id) - empty_bar) %>%
rowwise() %>%
mutate(title=mean(c(start, end)))
# prepare a data frame for grid (scales)
grid_data <- base_data
grid_data$end <- grid_data$end[ c( nrow(grid_data), 1:nrow(grid_data)-1)] + 1
grid_data$start <- grid_data$start - 1
grid_data <- grid_data[-1,]
I cannot made the graphic in R because I get an error.我无法在 R 中制作图形,因为出现错误。
Thank you谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.