Home > Mobile >  How to customize Horizontal dots plot?
How to customize Horizontal dots plot?

Time:05-21

I want to plot customized Horizontal dots using my data and the code given enter image description here

Expected outcome (smth. like this: enter image description here

CodePudding user response:

Try this.

First, reshaping so that both rate and change are in one column better supports ggplot's general preference towards "long" data.

df2 <- reshape2::melt(df, id.vars = c("origin", "Percentage"))

(That can also be done using pivot_wider.)

The plot:

ggplot(df2, aes(value, origin))  
  geom_label(aes(label = value, fill = variable, color = variable))  
  geom_point(aes(size = Percentage), x = max(df2$value)  
20, shape = 21)  
  scale_x_continuous(expand = expansion(add = c(15, 25)))  
  scale_fill_manual(values = c(change="lightblue", rate="blue"))  
  scale_color_manual(values = c(change="black", rate="white"))

enter image description here

The legend and labels can be adjusted in the usual ggplot methods. Overlapping of labels is an issue with which you will need to contend.

CodePudding user response:

Update on OP request: See comments:

gg_dot  
  geom_text(aes(x = rate, y = origin, 
                label = paste0(round(rate, 1), "%")),
            col = "black")  
  geom_text(aes(x = change, y = origin, 
                label = paste0(round(change, 1), "%")),
            col = "white")  
  geom_text(aes(x = x, y = y, label = label, col = label),
            data.frame(x = c(40 - 1.1, 180   0.6), y = 11, 
                       label = c("change", "rate")), size = 6)  
  scale_color_manual(values = c("#9DBEBB", "#468189"), guide = "none")  
  scale_y_discrete(expand = c(0.2, 0)) 

enter image description here

First answer: Something like this?

library(tidyverse)
library(dslabs)

gg_dot <- df %>% 
  arrange(rate) %>%
  mutate(origin = fct_inorder(origin)) %>% 
  ggplot()  
  # remove axes and superfluous grids
  theme_classic()  
  theme(axis.title = element_blank(),
        axis.ticks.y = element_blank(),
        axis.line = element_blank())  
  # add a dummy point for scaling purposes
  geom_point(aes(x = 12, y = origin), 
             size = 0, col = "white")  
  
  # add the horizontal discipline lines
  geom_hline(yintercept = 1:10, col = "grey80")  
  
  # add a point for each male success rate
  geom_point(aes(x = rate, y = origin), 
             size = 11, col = "#9DBEBB")  
  # add a point for each female success rate
  geom_point(aes(x = change, y = origin),
             size = 11, col = "#468189") 



gg_dot  
  geom_text(aes(x = rate, y = origin, 
                label = paste0(round(rate, 1))),
            col = "black")  
  geom_text(aes(x = change, y = origin, 
                label = paste0(round(change, 1))),
            col = "white")  
  geom_text(aes(x = x, y = y, label = label, col = label),
            data.frame(x = c(40 - 1.1, 180   0.6), y = 11, 
                       label = c("change", "rate")), size = 6)  
  scale_color_manual(values = c("#9DBEBB", "#468189"), guide = "none")  
  scale_y_discrete(expand = c(0.2, 0)) 

enter image description here

  • Related