Slyme
Slyme

Reputation: 131

Plotting a single variable on the y-axis and multiple on the x-axis

I am attempting to create a plot focused on two pieces. The first being a unique user_agent_string on the y-axis, and 5 other variables on the x-axis. This then would be faceted based on classification of the user_agent_string. I would then like to iterate through my user_agent_strings and plot each one with its respective variable values.

For reference I have provided a crude whiteboard drawing of what I would hope for.

Plot Format

Currently I am able to create this format: enter image description here

With this code:

col<-names(gbuas_focus[,6:11])

for(i in col) {
  print(i)
  ua_p<-ggplot(gbuas_focus,aes(color=duplicate)
  )+
    geom_point(aes_string(
      x=i,
      y='uas_id'),
      size=3,
    )+
    labs(x='Variable Score',
         y='User Agent Strings',
         title="Understanding Misclassifying Variables"
    )+
    facet_wrap(~classification)
  print(ua_p)
}

I would assume this could be done using multiple for loops, but I am having an egregious time figuring it out. Any thoughts?

EDIT: Here is the REAL dput output:

    structure(list(uas_id = c("UAS1", "UAS2", "UAS3", "UAS4", "UAS5", 
"UAS6", "UAS7", "UAS8", "UAS9", "UAS10", "UAS11", "UAS12", "UAS13", 
"UAS14", "UAS15", "UAS16", "UAS17", "UAS18", "UAS19", "UAS20"
), uas_count = c(1380544L, 1133471L, 830636L, 646202L, 433693L, 
432073L, 423441L, 313840L, 303530L, 270289L, 221152L, 201028L, 
178423L, 169872L, 142562L, 130877L, 128819L, 111131L, 102078L, 
96986L), classification = structure(c(1L, 1L, 2L, 1L, 1L, 1L, 
1L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L), .Label = c("Bot", 
"Human"), class = "factor"), duplicate = c("No", "No", "No", 
"No", "No", "No", "No", "No", "Yes", "No", "Yes", "No", "Yes", 
"No", "No", "No", "No", "Yes", "No", "Yes"), phantom_etsl = c(0L, 
0L, 2L, 0L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L), headchr_plugins = c(2L, 2L, 2L, 2L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), headchr_chrome_obj = c(2L, 
2L, 2L, 2L, 0L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L), headchr_permissions = c(2L, 2L, 2L, 2L, 2L, 2L, 
2L, 0L, 2L, 0L, 2L, 0L, 2L, 2L, 2L, 0L, 0L, 2L, 0L, 2L), chr_memory = c(2L, 
2L, 2L, 2L, 2L, 2L, 2L, 0L, 2L, 2L, 2L, 0L, 2L, 2L, 2L, 2L, 0L, 
2L, 2L, 2L), mq_screen = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L)), class = "data.frame", row.names = c(NA, 
-20L))

EDIT 2: With the help of @SimonWoodward I was able to configure the following code to give me at least most of what I needed.

gbuas_focus %>%
  gather(key, value, one_of(col))%>%
  ggplot() +
  geom_point(aes(x=key, y=value, color=duplicate), size=2) +
  coord_flip() +
  facet_wrap(~uas_id) +
  labs(y='Score', x='Variable', title="Understanding Misclassifying Variables")

enter image description here

Upvotes: 0

Views: 225

Answers (1)

Simon Woodward
Simon Woodward

Reputation: 2026

Something like this?

gbuas_focus <- structure(list(user_agent_string = structure(c(1L, 2L, 3L, 4L,
5L, 8L, 9L, 10L, 10L, 11L, 11L, 12L, 12L, 6L, 7L, 13L, 14L, 14L,
15L, 15L), .Label = c("Mozilla/5.0 (iPad; CPU OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148",
"Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148",
"Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1 Mobile/15E148 Safari/604.1",
"Mozilla/5.0 (iPhone; CPU iPhone OS 12_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148",
"Mozilla/5.0 (Linux; Android 9; SM-G960U Build/PPR1.180610.011; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/74.0.3729.157 Mobile Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
), class = "factor"), uas_id = c("UAS1", "UAS2", "UAS3", "UAS4",
"UAS5", "UAS6", "UAS7", "UAS8", "UAS9", "UAS10", "UAS11", "UAS12",
"UAS13", "UAS14", "UAS15", "UAS16", "UAS17", "UAS18", "UAS19",
"UAS20"), uas_count = c(1380544L, 1133471L, 830636L, 646202L,
433693L, 432073L, 423441L, 313840L, 303530L, 270289L, 221152L,
201028L, 178423L, 169872L, 142562L, 130877L, 128819L, 111131L,
102078L, 96986L), classification = structure(c(1L, 1L, 2L, 1L,
1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L
), .Label = c("Bot", "Human"), class = "factor"), duplicate = c("No",
"No", "No", "No", "No", "No", "No", "No", "Yes", "No", "Yes",
"No", "Yes", "No", "No", "No", "No", "Yes", "No", "Yes"), phantom_etsl = c(0L,
0L, 2L, 0L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L), headchr_plugins = c(2L, 2L, 2L, 2L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), headchr_chrome_obj = c(2L,
2L, 2L, 2L, 0L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L), headchr_permissions = c(2L, 2L, 2L, 2L, 2L, 2L,
2L, 0L, 2L, 0L, 2L, 0L, 2L, 2L, 2L, 0L, 0L, 2L, 0L, 2L), chr_memory = c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 0L, 2L, 2L, 2L, 0L, 2L, 2L, 2L, 2L, 0L,
2L, 2L, 2L), mq_screen = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L)), class = "data.frame", row.names = c(NA,
-20L))

library(tidyverse)

col<-names(gbuas_focus)[6:11]

gbuas_focus %>%
    gather(key, value, one_of(col)) %>%
    ggplot() +
    geom_point(aes(x=key, y=value, colour=duplicate), size=2) +
    coord_flip() +
    facet_grid(uas_id ~ classification) +
    labs(y='Score', x='Variable', title="Understanding Misclassifying Variables")

Created on 2019-06-13 by the reprex package (v0.3.0)

Upvotes: 1

Related Questions