0

Is it possible to add horizontal lines from 0 to the points on the plot shown below?

This is the code thus far:

ggplot(data, aes(x=change, y=industry, color=geo)) + geom_point() + 
scale_x_continuous(labels = scales::comma) + geom_vline(xintercept = 0)

Alternatively, I could use geom_bar() but I have been unsure how to show both London and the UK without them summing together.

image link

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
Blue94
  • 3
  • 4

1 Answers1

0

tl;dr you can use geom_bar() with position="stack", stat="identity". Or you can use geom_segment().

set up data

dd <- expand.grid(industry=c("property",
                            "manufacturing",
                            "other"),
                 geo=c("London","UK"))
set.seed(101)
dd$change <- runif(6,min=-30,max=30)

This is how you could do it with geom_bar

library(ggplot2)
ggplot(dd,aes(x=industry,y=change,
              fill=geo))+
  geom_bar(stat="identity",
           position="dodge")+
  coord_flip()

Or with geom_segment():

ggplot(dd,aes(x=change,y=industry,
              colour=geo))+
  geom_point(size=2)+
  geom_segment(aes(xend=0,yend=industry))

You might want to consider manually dodging the position in the second case, but position_dodge in ggplot can only dodge horizontally, so you should either switch x and y and use coord_flip(), or use position_dodgev from the ggstance package.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Thanks I will give that a try! Would you be able to explain the purpose of the `set.seed()` function when setting up the data? I understand it is for random number generation but further than that I'm unclear – Blue94 Mar 10 '19 at 21:04
  • have you looked around on the web? There's probably even a good explanation on SO somewhere: https://stackoverflow.com/questions/13605271/reasons-for-using-the-set-seed-function – Ben Bolker Mar 11 '19 at 00:54