简体   繁体   English

R ggplot气泡图本地化气泡显示没有在单个图表中

[英]R ggplot bubble chart localised bubbles display without in single chart

Hello R/ggplot experts!你好 R/ggplot 专家!

R and ggplot learner here. R 和 ggplot 学习者在这里。

I was working on a scenerio and was thinking, how I can display the data in best possible way.我正在制作一个场景,并在思考如何以最佳方式显示数据。 I need suggestion and direction from you guys.我需要你们的建议和指导。

R reproducible ggplot : R 可重现的 ggplot

library(ggrepel)

# Create the data frame.
sales_data <- data.frame(
  emp_name <- c("Sam", "Dave", "John", "Harry", "Clark", "Kent", "Kenneth", "Richard", "Clement", "Toby"), 
  month <- as.factor(c("Jan", "Feb", "Mar", "Jan", "Feb", "Mar", "Jan", "Feb", "Mar", "Jan")),
  dept_name <- as.factor(c("Production", "Services", "Support", "Support", "Services", "Production", "Production", "Support", "Support", "Support")), 
  revenue <- c(100, 200, 300, 400, 500, 600, 500, 400, 300, 200)
)

sales_data$month <- factor(sales_data$month, levels = c("Jan", "Feb", "Mar"))

categorical_bubble_chart <-  ggplot(sales_data, aes(x= month, y = dept_name, size = revenue, fill = revenue, label = revenue)) +
  geom_point(shape = 21, show.legend = FALSE) 

categorical_bubble_chart

Output plot is :输出图是 在此处输入图片说明

So far good!到目前为止还好! To represent the data in best possible way in present scenerio.在当前场景中以最佳方式表示数据。 Here is what it should look like.这是它应该是什么样子。 在此处输入图片说明

I am having difficulty in understanding following points:我很难理解以下几点:

  1. How I can show categories of month("Jan", "Feb", "Mar") in between grid lines.如何在网格线之间显示月份类别(“Jan”、“Feb”、“Mar”)。 Similarly for Departments.部门也是如此。 So that I can make a grid like region for each of the combination.这样我就可以为每个组合制作一个类似网格的区域。

  2. Right now, all bubbles are overlapping on each other.现在,所有气泡都相互重叠。 I want to put bubbles in non-overlapping manner.我想以不重叠的方式放置气泡。 For that I am thinking to add one more column in my data frame and randomly assign a value such that, it will be used to plot it inside the grid region.为此,我想在我的数据框中再添加一列并随机分配一个值,以便将其用于将其绘制在网格区域内。 But I am finding it difficult to understand, when my x/y are already month and dept_name then what random value I can provide to make each bubble different from each other ?但是我发现很难理解,当我的 x/y 已经是monthdept_name我可以提供什么随机值来使每个气泡彼此不同?

I have been thinking on it's solution since last 5-6 hours but couldn't find solution.自过去 5-6 小时以来,我一直在考虑它的解决方案,但找不到解决方案。 Any direction or suggestion would be highly appreciated and learning for future readers.任何方向或建议都将受到高度赞赏,并为未来的读者学习。

Are you looking for something like this?你在寻找这样的东西吗? I could not find the positioning of the bubbels for each facet in your data, so I took revenue.我无法在您的数据中找到每个方面的气泡定位,因此我获取了收入。

require(ggplot2)
# Create the data frame.
sales_data <- data.frame(
  emp_name = c("Sam", "Dave", "John", "Harry", "Clark", "Kent", "Kenneth", "Richard", "Clement", "Toby"), 
  month = as.factor(c("Jan", "Feb", "Mar", "Jan", "Feb", "Mar", "Jan", "Feb", "Mar", "Jan")),
  dept_name = as.factor(c("Production", "Services", "Support", "Support", "Services", "Production", "Production", "Support", "Support", "Support")), 
  revenue = c(100, 200, 300, 400, 500, 600, 500, 400, 300, 200)
)

sales_data$month <- factor(sales_data$month, levels = c("Jan", "Feb", "Mar"))

categorical_bubble_chart <-  ggplot(sales_data, aes(x= revenue, y = revenue, size = revenue, fill = revenue, label = revenue)) +
  geom_point(shape = 21, show.legend = FALSE) +
  facet_grid(dept_name~month)

categorical_bubble_chart

gives:给出:

在此处输入图片说明

As an alternative to the @Wietze314 approach, "quick & dirty" single-chart built:作为@Wietze314 方法的替代方法,构建了“快速而肮脏”的单图表:

ggplot(data = sales_data, aes(x = month, y = dept_name)) +
  geom_tile(data = expand.grid(sales_data$month, sales_data$dept_name), 
            aes(x = Var1, y = Var2), fill = NA, col = 'gray50', lty = 2) +
  geom_point(aes(size = revenue, col = revenue), 
             shape = 16, position = position_jitter(seed = 0), show.legend = F) +
  geom_text(aes(label = revenue), vjust = 1.6, position = position_jitter(seed = 0)) +
  theme_bw() +
  theme(
    axis.title = element_blank(),
    axis.ticks = element_blank(),
    plot.background = element_blank(), 
    axis.line = element_blank(), 
    panel.border = element_blank(), 
    panel.grid = element_blank()
    )

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM