简体   繁体   English

制作圆形堆积条形图

[英]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.观察值是ORIGENFROM列。 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.

相关问题 如何将此堆叠条形图转换为 R 中的圆形 plot? - How can I convert this stacked barplot to a circular plot in R? 如何使用嵌套分组变量制作堆积条形图? - How to make a stacked barplot with nested grouping variables? 重塑数据以在R中使用ggplot制作堆叠式barplot - Reshape data to make stacked barplot with ggplot in R 如何在ggplot2中使用水平和垂直堆叠的条形图制作条形图? - How to make a barplot in ggplot2 with both horizontal and vertical stacked bars? 如何使用ggplot2制作堆叠式barplot - how can I make stacked barplot with ggplot2 有没有一种简单的方法可以用 R 制作水平单堆叠条形图? - Is there an easy way to make an horizontal single stacked Barplot with R? 如何在堆叠的条形图上叠加条形图? - How to overlay a barplot on a stacked barplot? 如何为每个条制作具有定义的水平边框的堆叠条形图 - how to make stacked barplot with defined horizontal borders for each bar R 中的圆形堆叠条形图 - 美学长度必须为 1 或与数据相同 (26) - Circular stacked barplot in R - Aesthetics must be either length 1 or the same as the data (26) 如何使用三列在 R 中制作堆叠条形图,我想使用 barplot() 函数 - How do I make stacked barplot in R utilising three columns, I want to use the barplot() function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM