Hello R/ggplot experts!
R and ggplot learner here.
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:
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
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:
How I can show categories of month("Jan", "Feb", "Mar") in between grid lines. Similarly for Departments. So that I can make a grid like region for each of the combination.
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 ?
I have been thinking on it's solution since last 5-6 hours but couldn't find solution. Any direction or suggestion would be highly appreciated and learning for future readers.
As an alternative to the @Wietze314 approach, "quick & dirty" single-chart built:
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()
)