使用 packcircles 创建气泡图我想要圆形而不是椭圆形加上图例 ggplot

问题描述 投票:0回答:1

我想使用 packcircles 包和 ggplot 创建一个图表。并用我的导出它 我在这种情况下使用的打印设备“ragg”。

我有三个问题

  1. 打印时我想要的圆形会转换为椭圆形
  2. 气泡内的文本,我希望能够调整其大小,使其适合气泡的大小。 小气泡的尺寸为 14,几乎不易察觉。
  3. 我希望我的图例具有我在数据帧“数据”中定义的段,而不是我得到“id”数字。

我试过这个:

library(tidyverse)
library(ggthemes)
library(scales)
library(ggtext)
library(ggrepel)
library(ragg)    
library(systemfonts)   
library(magick)
library(png)
library(ggnewscale) 
library(ggh4x)
library(packcircles)

theme_b <- function( ) {
  
  theme(
    panel.background = element_rect(fill ="white"),
    panel.grid.major.y  = element_line(color = "white"),
    panel.grid.major.x  = element_blank(),
    panel.grid.minor = element_blank(),
    axis.ticks  = element_blank(),
    axis.ticks.length.x = unit(0.15,"cm"), 
    plot.background = element_rect(fill="white"),
    legend.background = element_rect(fill="transparent"),
    legend.key = element_blank(),
    axis.title.x = element_blank(),
    axis.text = element_blank(),

    plot.title = element_textbox(size=32,color="black",vjust=1,
                                 family = "merriweather",width = unit(1, "npc"),
                                 margin= margin(r=-40,b=80),
                                 lineheight = 1.3),
    plot.caption.position = "plot",
    plot.caption = element_textbox(size=12,color="black",margin= margin(t = 10, b=10,r=60),
                                   lineheight = 1.2,
                                   family = "montserrat",
                                   width = unit(1, "npc")),
    
    axis.title.y.right = element_blank(),
    axis.title.y.left = element_blank(),
    axis.line.x = element_blank(),
    plot.margin = unit(c(1,2,0,1),"cm"),
    legend.direction = "horizontal",
    legend.spacing.x = unit(0.4, 'cm'),
    legend.position = c(.30, 1.06),
    legend.text = element_text(size = 13,color="black",
                               margin = margin(l = -10)),
    legend.title = element_markdown(size = 13,
                                    color = "black",
                                    family = "montserrat",
                                    lineheight = 1.2),

    
    
  )
}



# Create data
data <- data.frame(Segment=c("Small","Medium", "Large", "Extra Large", 
                                    "Extra extra large"), 
                          value=c(10000, 3000, 400, 300, 70)) 
packing <- circleProgressiveLayout(data$value, sizetype='area')

data <- cbind(data, packing)

data_gg <- circleLayoutVertices(packing, npoints=50)


tp <- "Clothing Size Inventory"
c1 <- "Source: Company"
lnd <- "**Size table"


p <- ggplot() + 
  
  # Make the bubbles
  geom_polygon(data = data_gg, 
               aes(x, y, group = id, fill=as.factor(id)), alpha = 0.6) +
  labs(title = tp,
       caption = c1,
       fill = lnd) +
  scale_fill_manual(values = c("#edf8b1","#c7e9b4","#7fcdbb","#41b6c4",
                               "#1d91c0"),
                    guide = guide_legend(title.position = "top",ncol = 2)) + 
  guides(fill=guide_legend(title.position = "top",
                           byrow = TRUE,
                           nrow = 2),
         alpha="none",
         size = "none") + 
  geom_text(data = data, 
            aes(x, y, size=c(30,24,15,15,14), label = round(value/1000,2))) +
  scale_size_continuous(range = c(1,5)) +
  
  # General theme:
  theme_b() + 
  coord_equal()



p2 <- p + 
  coord_cartesian(clip = "off")


agg_png("bubbles_example2.png", width = 8, height = 8, units = "in", res = 300)
p2
dev.off()

我明白了: bubble chart

我想要这个: bubble chart desired

r ggplot2 charts r-package bubble-chart
1个回答
1
投票

圆圈显示为椭圆形,因为您用

coord_equal
覆盖了
coord_cartesian
。填充图例显示圆圈 ID,因为您已将其映射到填充美学。文本大小与您认为指定的大小不同,因为您使用了范围为 1-5 的
scale_size_continuous
,因此文本将始终在该范围内。您可能想要
scale_size_identity

将所有这些放在一起,我们有:

ggplot(data_gg, aes(x, y)) + 
  geom_polygon(aes(group = id, fill = factor(id)), alpha = 0.6) +
  labs(title = tp, caption = c1, fill = lnd) +
  scale_fill_manual(values = c("#edf8b1", "#c7e9b4", "#7fcdbb", 
                               "#41b6c4", "#1d91c0"), labels = data$Segment) + 
  guides(fill = guide_legend(title.position = "top", byrow = TRUE, nrow = 2),
         size = "none") + 
  geom_text(data = data, 
            aes(size = c(30, 24, 15, 15, 10)/2, label = round(value/1000,2))) +
  scale_size_identity() +
  theme_b() + 
  coord_equal()

© www.soinside.com 2019 - 2024. All rights reserved.