rggplot2ggraph

Two columns on x-axis and different grids in R


I need some help with a graph in R.

This is how my dataframe looks like

Footprint Local Number Remote Number Location
10.4 45 4 L1
12.5 452 78 L9
15.6 86 52 L5
85.3 12 12 L4
12.5 35 36 L2
85.9 78 78 L3
78.5 44 44 L6
4.6 10 11 L7
13.9 157 2 L8

What I want to achieve is a graph with the 'Footprint' column in the y-axis, the 'Local Number' column(in the x-axis) in the positive grid of the graph and the 'Remote Number' column(in the x-axis) in the negative grid of the graph. The data should be presented in dots and the lab name should be the label. So basically, I want to show for each location the remote and local number of employees. I am struggling on presenting the two columns in the x-axis. I appreciate the help!


Solution

  • Maybe you want something like where you could use geom_point for both columns with one negative and positive and add labels using geom_text like this:

    df <- read.table(text = 'Footprint  Local_Number    Remote_Number   Location
    10.4    45  4   L1
    12.5    452 78  L9
    15.6    86  52  L5
    85.3    12  12  L4
    12.5    35  36  L2
    85.9    78  78  L3
    78.5    44  44  L6
    4.6 10  11  L7
    13.9    157 2   L8
    ', header = TRUE)
    
    library(ggplot2)
    ggplot() +
      geom_point(df, mapping = aes(x = Footprint, y = Local_Number, color = '1')) +
      geom_point(df, mapping = aes(x = -Remote_Number, y = Local_Number, color = '2')) +
      geom_text(df, mapping = aes(x = Footprint, y = Local_Number, label = Location), hjust = 0, vjust = 0) +
      geom_text(df, mapping = aes(x = -Remote_Number, y = Local_Number, label = Location), hjust = 0, vjust = 0) +
      scale_color_manual('Legend', labels = c('Footprint', 'Remote number'), values = c('blue', 'red')) +
      labs(y = 'Local Number')
    

    Created on 2022-10-14 with reprex v2.0.2


    If you want to show it on only a positive axis you could the negative sign like this:

    library(ggplot2)
    ggplot() +
      geom_point(df, mapping = aes(x = Footprint, y = Local_Number, color = '1')) +
      geom_point(df, mapping = aes(x = Remote_Number, y = Local_Number, color = '2')) +
      geom_text(df, mapping = aes(x = Footprint, y = Local_Number, label = Location), hjust = 0, vjust = 0) +
      geom_text(df, mapping = aes(x = Remote_Number, y = Local_Number, label = Location), hjust = 0, vjust = 0) +
      scale_color_manual('Legend', labels = c('Footprint', 'Remote number'), values = c('blue', 'red')) +
      labs(y = 'Local Number')
    

    Created on 2022-10-14 with reprex v2.0.2