1

I have the following chart made in Tableau, and would like to replicate it in R:

Expected final result I have extracted the data from the chart in Tableau and this is how the first 10 rows of data looks like;

structure(list(ID = structure(c(2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 
1L, 1L), .Label = c("015753e0-8574-4b13-88d7-627292d52272", "60a7614a-63bb-4bb9-91ac-520549853c19", 
"86735a77-b822-4320-b16e-1ff2b5535f5d"), class = "factor"), NarrID = c(173L, 
173L, 174L, 174L, 174L, 174L, 174L, 174L, 175L, 175L), Stone.Num = c(1L, 
99L, 1L, 2L, 3L, 4L, 5L, 99L, 4L, 99L), NarrID_Stone = c(173.01, 
173.99, 174.01, 174.02, 174.03, 174.04, 174.05, 174.99, 175.04, 
175.99), Stone.Name.L1 = structure(c(4L, 3L, 4L, 5L, 1L, 6L, 
2L, 3L, 6L, 3L), .Label = c("District Officials", "Malik / Wakil-e-Guzar", 
"Placeholder", "Police", "Provincial Officials", "Religious Leaders"
), class = "factor"), Canvas01.AdjXRightValue = c(0.7299, NA, 
0.8994, 0.8615, 0.2399, 0.9103, NA, NA, 0.8757, NA), Canvas01.AdjYTopValue = c(0.2903, 
NA, 0.8374, 0.6583, 0.8183, 0.2167, NA, NA, 0.8374, NA), Canvas01.Quadrant = structure(c(3L, 
1L, 4L, 4L, 2L, 3L, 1L, 1L, 4L, 1L), .Label = c("", "2.  Upper Left", 
"3.  Lower Right", "4.  Upper Right"), class = "factor"), Canvas02.AdjXRightValue = c(0.2113, 
NA, 0.8892, 0.8102, 0.1629, 0.8911, NA, NA, 0.9206, NA), Canvas02.AdjYTopValue = c(0.2375, 
NA, 0.8305, 0.7514, 0.8385, 0.1948, NA, NA, 0.2521, NA), Canvas02.Quadrant = structure(c(2L, 
1L, 5L, 5L, 3L, 4L, 1L, 1L, 4L, 1L), .Label = c("", "1. Lower Left", 
"2.  Upper Left", "3.  Lower Right", "4.  Upper Right"), class = "factor"), 
    Canvas03.AdjXRightValue = c(0.2945, NA, 0.8503, 0.7177, 0.2971, 
    0.9026, NA, NA, 0.1442, NA), Canvas03.AdjYTopValue = c(0.2849, 
    NA, 0.7296, 0.7228, 0.7445, 0.1675, NA, NA, 0.836, NA), Canvas03.Quadrant = structure(c(2L, 
    1L, 5L, 5L, 3L, 4L, 1L, 1L, 3L, 1L), .Label = c("", "1. Lower Left", 
    "2.  Upper Left", "3.  Lower Right", "4.  Upper Right"), class = "factor"), 
    Canvas04.AdjXRightValue = c(0.2804, NA, 0.9165, 0.8147, 0.7183, 
    0.1924, 0.6477, NA, 0.6946, NA), Canvas04.AdjYTopValue = c(0.2808, 
    NA, 0.8114, 0.7282, 0.7064, 0.2767, 0.7391, NA, 0.746, NA
    ), Canvas04.Quadrant = structure(c(2L, 1L, 3L, 3L, 3L, 2L, 
    3L, 1L, 3L, 1L), .Label = c("", "1. Lower Left", "4.  Upper Right"
    ), class = "factor"), Canvas05.AdjXRightValue = c(0.2817, 
    NA, 0.2078, 0.2065, 0.1205, 0.2958, 0.1166, NA, 0.1115, NA
    ), Canvas05.AdjYTopValue = c(0.2535, NA, 0.2167, 0.3122, 
    0.1455, 0.2399, 0.3094, NA, 0.8169, NA), Canvas05.Quadrant = structure(c(2L, 
    1L, 2L, 2L, 2L, 2L, 2L, 1L, 3L, 1L), .Label = c("", "1. Lower Left", 
    "2.  Upper Left"), class = "factor")), row.names = c(NA, 
10L), class = "data.frame")
Z.Lin
  • 28,055
  • 6
  • 54
  • 94
  • Please share data in a reproducible format (e.g. using `dput`); a screenshot of data/code is never a good idea because it's not easy to extract data from an image. Take a look at what you can do to provide a [minimal reproducible example/attempt](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Maurits Evers May 14 '19 at 08:05
  • @MauritsEvers I have added the data, please review and advice. Thanks. – Jonathan Munene May 14 '19 at 08:21
  • 1
    It's still not clear *what* you're trying to plot. What do you want to show on the x-axis? What on the y-axis? Your data contains a lot of columns. Which ones are relevant? What have you tried so far? Sharing your code attempt is often very useful to help us understand what it is that you're trying to do. – Maurits Evers May 14 '19 at 08:30

1 Answers1

1

I'm not exactly sure what you are looking to do but you're likely going to need your data in a long format. To filter for a different canvas version, you'd have to look into something like flexdashboard or shiny for the user to select or a loop if you're building a report.

See if this can help you conceptualize a possible solution

df_long <-
  df %>%
  select(
    id = ID, 
    name = Stone.Name.L1, 
    contains("Canvas")
  ) %>% 
  # convert to long everything but id & name
  gather(orig, value, -c(id, name)) %>% 
  # break the canvas and metric into 2 columns on the "."
  separate(orig, into = c("canvas", "metric"), sep = "\\.") %>% 
  # covert to wide
  spread(metric, value) %>% 
  rename(
    x = AdjXRightValue,
    y = AdjYTopValue,
    quad = Quadrant
  ) %>% 
  filter(!is.na(x)) %>% 
  # some modifications to the vars
  mutate(
    x = as.numeric(x),
    y = as.numeric(y),
    quad = str_extract(quad, "(Low|Upp).*")
  ) %>% 
  # break quad into 2 columns (not sure you need this)
  separate(quad, c("vert", "horiz"), remove = FALSE)

and then the plot

ggplot(df_long, aes(x, y, color = quad)) +
  geom_point(size = 2) +
  geom_hline(yintercept = 0.5) +
  geom_vline(xintercept = 0.5) +
  theme_minimal() +
  theme(
    legend.position = "none"
  )

enter image description here

yake84
  • 3,004
  • 2
  • 19
  • 35