Home > database >  Two columns on x-axis and different grids in R
Two columns on x-axis and different grids in R

Time:10-15

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!

CodePudding user response:

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

CodePudding user response:

Here two more suggestions for visualisation. This seems to be paired data - remote vs local number. That can be either represented as a scatter plot or as change.

Footprint can then be encoded as color. Thanks 1 to Quieten for the data.

library(tidyverse)

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)

df %>%
  ggplot(aes(Local_Number, Remote_Number))  
  ## use Number as x and y and color code by footprint value
  geom_point(aes(color = Footprint), size = 3)  
  ## label the points, best with repel
  ggrepel::geom_text_repel(aes(label = Location))  
  ## optional add a line of equality to help intuitive recognition of change
  ##   keeping same limits helps intuitive comparison
  geom_abline(intercept = 0, slope = 1, lty = 2, size = .3)  
  coord_equal(xlim = range(c(df$Local_Number, df$Remote_Number)), ylim = range(c(df$Local_Number, df$Remote_Number)))  
  ## optional change color scale
  scale_color_viridis_c(option = "magma") 

  
## or, not to waste half of your graph (there is no positive value)
## you can show the difference instead
df %>%
  mutate(change = Local_Number-Remote_Number) %>%
  ggplot()  
  ## now use Location as x variable, therefore no labels needed any more
  geom_point(aes(Location, change, color = Footprint), size = 3)  
 ## optional change color scale
  scale_color_viridis_c(option = "magma") 

Created on 2022-10-14 by the reprex package (v2.0.1)

  • Related