如何在ggplot中创建气泡网格图? [英] How can I create bubble grid charts in ggplot?

查看:53
本文介绍了如何在ggplot中创建气泡网格图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用ggplot创建气泡网格图.像这样的东西:

I want to create bubble grid charts with ggplot. somthing like this:

我在网上找不到任何代码或示例.

I couldnt find any code or exampe online.

谢谢

推荐答案

使用具有离散x和y比例的 geom_point 可以帮助您入门.这是一个包含一些快速玩具数据的示例:

Using geom_point with discrete x and y scales will get you started. Here's an example with some quick toy data:

library(tidyverse)

offenses <- c("robbery", "violence", "drugs")
actions <- c("formal", "informal", "considered")
counts <- sample(10:100, 9, replace = TRUE)

data <- expand.grid(offenses = offenses, actions = actions) %>% bind_cols(counts = counts)

ggplot(data,
       aes(x = str_to_title(offenses), 
           y = str_to_title(actions),
           colour = str_to_title(offenses),
           size = counts)) +
  geom_point() +
  geom_text(aes(label = counts), 
            colour = "white", 
            size = 3) +
  scale_x_discrete(position = "top") +
  scale_size_continuous(range = c(10, 30)) + # Adjust as required.
  scale_color_brewer(palette = "Set2") +
  labs(x = NULL, y = NULL) +
  theme(legend.position = "none",
        panel.background = element_blank(),
        panel.grid = element_blank(),
        axis.ticks = element_blank())

根据需要使用 scale_size_continouous range 参数来获得适合您数据集大小的气泡.

Play around with the range parameter of scale_size_continouous as needed to get bubbles of a reasonable size for your data set.

这篇关于如何在ggplot中创建气泡网格图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆