首页 > 解决方案 > 带有圆形条形图的填充面板

问题描述

我正在研究在网上找到的一些用于打印圆形条形图的代码块。我无法“缩放”或减少空白以获得更大的图表。我附上一张图片,我想至少在图表上方和下方减少区域(在红色区域中)。请问有什么提示吗?

带有红色“操纵”目标区域的图表:

在此处输入图像描述

ps抱歉代码乱七八糟,只是在试验......

source("code_wrang_eda_danger.R")
medie <- round(rowMeans(df_danger_Totals[, 2:12]), digits = 3)
tab1 <- cbind(df_danger_Totals,medie)
tab2 <-tibble::rowid_to_column(tab1, "id")
tab3 <- tab2 %>% 
        select(id, cer, medie) 

label_data <- tab3
number_of_bar <- nrow(label_data)
angle <-  90 - 360 * (label_data$id-0.5) /number_of_bar    
label_data$hjust<-ifelse( angle < -90, 1, 0)
label_data$angle<-ifelse(angle < -90, angle+180, angle)

p <- ggplot(tab3, aes(x=as.factor(id), y=medie)) +
        geom_bar(stat="identity", fill=alpha("skyblue", 0.7)) +
        
        # Limits of the plot = very important. The negative value controls the size of the inner circle, the positive one is useful to add size over each bar
        ylim(-380,450) +
        
        # Custom the theme: no axis title and no cartesian grid
        #theme_minimal() +
        theme(
                axis.text = element_blank(),
                axis.title = element_blank(),
                panel.grid = element_blank(),
                #plot.margin = margin(0.5, 0.50, 0.5, 0.5, "cm"),
                plot.background = element_rect(fill = "white"),
                panel.background = element_rect(fill = "red",
                                                colour = "red"),
                axis.ticks = element_blank(),
                panel.spacing.x = unit(0.5, "lines"),
                panel.spacing.y = unit(0.5, "lines"), 
                plot.margin = margin(t = 5,  # Top margin
                                     r = 5,  # Right margin
                                     b = 5,  # Bottom margin
                                     l = 5) # Left margin
        ) +
        
        # This makes the coordinate polar instead of cartesian.
        coord_polar(start = 0) +
        
        # Add the labels, using the label_data dataframe that we have created before
        geom_text(data=label_data, aes(x=id, y=medie+10, label=cer, hjust=hjust), color="black", fontface="bold",alpha=0.6, size=2.5, angle= label_data$angle, inherit.aes = FALSE ) 


p <- p + labs(title = "xxx",
              subtitle = "xxx",
              caption = "xxx") +
        theme(
                plot.title = element_text(hjust = 0.5, size = 14),    # Center title position and size
                plot.subtitle = element_text(hjust = 0.5),            # Center subtitle
                plot.caption = element_text(hjust = 0.5, face = "italic")# move caption to the left
        ) 

ggsave(path = "charts","pog_waste_coll_2008_2018_danger_circ_barplot.png", width = 5, height = 5)

p

标签: rggplot2

解决方案


推荐阅读