1

I need to add data labels with % signs to my dumbbell plot. Any idea how to do this? I have code that attempts to do this, but get the error "Error in x * 100 : non-numeric argument to binary operator". Also is it possible to increase the font of the statements on the graph? See my code below, and data and graph here

data: https://gofile.io/d/k8vadK graph: https://i.stack.imgur.com/2SExW.jpg

blue <- "#0171CE"
red <- "#DE4433"
percent_first <- function(x) {
  x <- sprintf("%d%%", round(x*100))
  x[2:length(x)] <- sub("%$", "", x[2:length(x)])
  x
}
library(ggplot2) 
library(ggalt)   
library(tidyverse)

ggplot() +
  geom_segment(data=overall, aes(y=statement, yend=statement, x=0.8, xend=1), color="#b2b2b2", size=0.15) +
geom_dumbbell(data=overall, aes(y=statement, x=prior, xend=current),
              size=1.5, color="#b2b2b2", size_x=3, size_xend = 3, colour_x = red, colour_xend = blue) +
geom_text(data=filter(overall, statement=="The meeting was well organized"),
          aes(x=current, y=statement, label="2020"),
          color=blue, size=3, vjust=-1.5, fontface="bold", family="Arial") +
  geom_text(data=filter(overall, statement=="The meeting was well organized" ),
            aes(x=prior, y=statement, label="2019"),
            color=red, size=3, vjust=-1.5, fontface="bold", family="Arial") +
geom_text(data=overall, aes(x=prior, y=statement, label=percent_first(rep)),
          color=red, size=2.75, vjust=2.5, family="Arial") +
  geom_text(data=overall, color=blue, size=2.75, vjust=2.5, family="Arial",
            aes(x=prior, y=statement, label=percent_first(dem)))


freeazabird
  • 347
  • 2
  • 11

1 Answers1

0

The data file is no longer available so here is some data I got from another site Get some data: Example data taken from https://plotly.com/r/dumbbell-plots/

It might be helpful if you still need help here or could help someone else

Thanks

##Get some data
s <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/school_earnings.csv")
s$School <- factor(s$School, levels = s$School[order(s$Men)])
s1<-s %>% mutate("type" = 'Gender earnings disparity') 

##Use the Dumbbell R package to Plot



##Plot
plot1<-dumbbell::dumbbell(xdf=s1,id="School",key = "type", column1 = "Women", column2 = "Men", delt=1 ,arrow = 1, lab1 = "Women", lab2="Men", p_col1 = "red", p_col2 = "blue" ,pt_val = 1 , pval = 2, textsize = 3) + 
  xlim(60,170) + 
  facet_wrap( . ~ type)

dumbbell R Pacakge

Foo Cheung
  • 41
  • 2