R语言绘制环形图,不停的增加半轴

R语言
在这里插入图片描述

# 加载包 ---------------------------------------------------------------------


library(tidyverse)
install.packages('ragg')
library(ragg)



# 创建一个数据 ------------------------------------------------------------------


data <- data.frame(
  individual=paste( "Mister ", seq(1,60), sep=""),
  group=c( rep('A', 10), rep('B', 30), rep('C', 14), rep('D', 6)) ,
  value=sample( seq(10,100), 60, replace=TRUE)
)

data<-test_dict_df

test_dict_df
#individual group value
# 接下来就是处理数据,对数据做转换 --------------------------------------------------------


datagroup <- data$bert %>% unique()

#datagroup

allplotdata <- tibble('bert' = datagroup,
                      'Indexs' = paste0('empty_individual_', seq_along(datagroup)),
                      'Tweets' = 0) %>% 
  bind_rows(data) %>% arrange(bert) %>% mutate(xid = 1:n()) %>% 
  mutate(angle = 90 - 360 * (xid - 0.5) / n()) %>% 
  mutate(hjust = ifelse(angle < -90, 1, 0)) %>% 
  mutate(angle = ifelse(angle < -90, angle+180, angle)) 


# 这个是提取出空的数据,做一些调整 --------------------------------------------------------
allplotdata

firstxid <- which(str_detect(allplotdata$Indexs, pattern = "empty_individual"))

firstxid

segment_data <- data.frame('from' = firstxid + 1,
                           'to' = c(c(firstxid - 1)[-1], nrow(allplotdata)),
                           'label' = datagroup) %>% 
  mutate(labelx = as.integer((from + to)/2))

# 这个是自定坐标轴 ----------------------------------------------------------------


coordy <- tibble('coordylocation' = seq(from = min(allplotdata$Tweets), to = max(allplotdata$Tweets), 10),
                 'coordytext' = as.character(round(coordylocation, 2)),
                 'x' = 1)

# 这个是自定义坐标轴的网格 ------------------------------------------------------------


griddata <- expand.grid('locationx' = firstxid[-1], 'locationy' = coordy$coordylocation)


# 这个就是开始画图了 ---------------------------------------------------------------


p <- ggplot() + 
  geom_bar(data = allplotdata, aes(x = xid, y = value, fill = group), stat = 'identity') + 
  geom_text(data = allplotdata %>% filter(!str_detect(individual, pattern = "empty_individual")), 
            aes(x = xid, label = individual, y = value+10, angle = angle, hjust = hjust),
            color="black", fontface="bold",alpha=0.6, size=2.5) + 
  geom_segment(data = segment_data, aes(x = from, xend = to), y = -5, yend=-5) + 
  geom_text(data = segment_data, aes(x = labelx, label = label), y = -15) + 
  geom_text(data = coordy, aes(x = x, y = coordylocation, label = coordytext),
            color="grey", size=3 , angle=0, fontface="bold") + 
  geom_segment(data = griddata, 
               aes(x = locationx-0.5, xend = locationx + 0.5, y = locationy, yend = locationy),
               colour = "grey", alpha=0.8, size=0.6) + 
  scale_x_continuous(expand = c(0, 0)) + 
  scale_y_continuous(limits = c(-50,100)) + 
  coord_polar() +
  theme_void() +
  labs(title = "pypi") + 
  theme(legend.position = 'none')


p <- ggplot() + 
  geom_bar(data = allplotdata, aes(x = xid, y = Tweets, fill = group), stat = 'identity') + 
  geom_text(data = allplotdata %>% filter(!str_detect(Tweets, pattern = "empty_individual")), 
            aes(x = xid, label = Tweets, y = Tweets+10, angle = angle, hjust = hjust),
            color="black", fontface="bold",alpha=0.6, size=2.5) + 
  geom_segment(data = segment_data, aes(x = from, xend = to), y = -5, yend=-5) + 
  geom_text(data = segment_data, aes(x = labelx, label = label), y = -15) + 
  geom_text(data = coordy, aes(x = x, y = coordylocation, label = coordytext),
            color="grey", size=3 , angle=0, fontface="bold") + 
  geom_segment(data = griddata, 
               aes(x = locationx-0.5, xend = locationx + 0.5, y = locationy, yend = locationy),
               colour = "grey", alpha=0.8, size=0.6) + 
  scale_x_continuous(expand = c(0, 0)) + 
  scale_y_continuous(limits = c(-50,100)) + 
  coord_polar() +
  theme_void() +
  labs(title = "pypi") + 
  theme(legend.position = 'none')

p


# 这里就是保存了 -----------------------------------------------------------------


ggsave(filename = 'circularbar2.png', plot = p, width = 10, height = 10, device = ragg::agg_png())

#绘制圆心图
# library
library(tidyverse)
install.packages("tidyverse")
# Create dataset
data <- data.frame(
  individual=paste( "Mister ", seq(1,60), sep=""),
  group=c( rep('A', 10), rep('B', 30), rep('C', 14), rep('D', 6)) ,
  value=sample( seq(10,100), 60, replace=T)
)

# Set a number of 'empty bar' to add at the end of each group
empty_bar <- 3
to_add <- data.frame( matrix(NA, empty_bar*nlevels(data$group), ncol(data)) )
colnames(to_add) <- colnames(data)
to_add$group <- rep(levels(data$group), each=empty_bar)
data <- rbind(data, to_add)

library(magrittr)

data <- data %>% arrange(group)
data$id <- seq(1, nrow(data))

# Get the name and the y position of each label
label_data <- data
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(group) %>% 
  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,]

# Make the plot
p <- ggplot(data, aes(x=as.factor(id), y=value, fill=group)) +       # Note that id is a factor. If x is numeric, there is some space between the first bar
  
  geom_bar(aes(x=as.factor(id), y=value, fill=group), stat="identity", alpha=0.5) +
  
  # Add a val=100/75/50/25 lines. I do it at the beginning to make sur barplots are OVER it.
  geom_segment(data=grid_data, aes(x = end, y = 80, xend = start, yend = 80), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
  geom_segment(data=grid_data, aes(x = end, y = 60, xend = start, yend = 60), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
  geom_segment(data=grid_data, aes(x = end, y = 40, xend = start, yend = 40), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
  geom_segment(data=grid_data, aes(x = end, y = 20, xend = start, yend = 20), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
  
  # Add text showing the value of each 100/75/50/25 lines
  annotate("text", x = rep(max(data$id),4), y = c(20, 40, 60, 80), label = c("20", "40", "60", "80") , color="grey", size=3 , angle=0, fontface="bold", hjust=1) +
  
  geom_bar(aes(x=as.factor(id), y=value, fill=group), stat="identity", alpha=0.5) +
  ylim(-100,120) +
  theme_minimal() +
  theme(
    legend.position = "none",
    axis.text = element_blank(),
    axis.title = element_blank(),
    panel.grid = element_blank(),
    plot.margin = unit(rep(-1,4), "cm") 
  ) +
  coord_polar() + 
  geom_text(data=label_data, aes(x=id, y=value+10, label=individual, hjust=hjust), color="black", fontface="bold",alpha=0.6, size=2.5, angle= label_data$angle, inherit.aes = FALSE ) +
  
  # Add base line information
  geom_segment(data=base_data, aes(x = start, y = -5, xend = end, yend = -5), colour = "black", alpha=0.8, size=0.6 , inherit.aes = FALSE )  +
  geom_text(data=base_data, aes(x = title, y = -18, label=group), hjust=c(1,1,0,0), colour = "black", alpha=0.8, size=4, fontface="bold", inherit.aes = FALSE)

p

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值