Home > Enterprise >  Utilising multiple columns to be the x-axis in ggplot2 for R
Utilising multiple columns to be the x-axis in ggplot2 for R

Time:10-23

Desired graph

I wanted to obtain the above graph, and I tried using ggplot for it.

blue = c(83, 81, 80, 71, 69, 63, 63, 62, 54)
red = c(112, 96, 111, 141, 125, 89, 178, 107, 130)
loc = c("YISHUN", "WOODLANDS", "HOUGANG", "ANG MO KIO"  "TAMPINES", "SENGKANG", "GEYLANG", "BEDOK", "JURONG WEST")
tib = mutate(blue, red, loc)
ggplot(lst_compare, aes(x=c(wet, dry) ,y=loc))  
  geom_point()   geom_segment(y=loc, x= wet, xend= dry, yend= loc)

I tried the above method, however, it seems I can't simply include multiple columns as the x value. Does anyone know how to include multiple columns as part of x-axis?

CodePudding user response:

I fixed the mistakes in your reprex. I think it's best to pivot the data before plotting.

library(tidyverse)
blue = c(83, 81, 80, 71, 69, 63, 63, 62, 54)
red = c(112, 96, 111, 141, 125, 89, 178, 107, 130)
loc = c("YISHUN", "WOODLANDS", "HOUGANG", "ANG MO KIO",  "TAMPINES", "SENGKANG", "GEYLANG", "BEDOK", "JURONG WEST")
tib = data.frame(blue, red, loc)

tib %>% 
  pivot_longer(c("blue", "red")) %>% 
  ggplot()  
  geom_point(aes(x = value, y = loc, color = name), size = 2)  
  scale_color_identity()  
  geom_line(aes(x = value, y = loc))   
  theme_minimal()

Created on 2021-10-21 by the reprex package (v2.0.1)

  • Related