ltrd
ltrd

Reputation: 326

Specific plot for classification visualization

Suppose that I have 5 variables for each observation (12 observation in a dataset) and I classify those observation to 3 class:

set.seed(123)

Dataset1 <- data.table(v1 = rnorm(12,-0.41,3.4),
                   v2 = rgamma(12,3,1.5),
                   v3 = rbeta(12,9,11),
                   v4 = rnig(12,12,33,23,13),
                   v5 = rpois(12,11),
                   class = floor(runif(12,1,4)))

I would like to visualize my results just like that:

enter image description here

Is it possible to do such a visualization in ggplot? I have no idea how to do it. Suppose that we have normalized every observation.

Upvotes: 0

Views: 670

Answers (1)

missuse
missuse

Reputation: 19716

This can be done in ggplot2 by first reshaping your data to long format and using geom_point for the plotting. Here is an example:

I omitted rnig from the data since it was producing errors

Dataset1 <- data.frame(v1 = rnorm(12,-0.41,3.4),
                       v2 = rgamma(12,3,1.5),
                       v3 = rbeta(12,9,11),
                       v5 = rpois(12,11),
                       class = floor(runif(12,1,4)))

library(tidyverse)

Dataset1 %>%
  gather(key, value, 1:4) %>% #convert to long format
  ggplot()+
  geom_point(aes(x = key, y = value, color = as.factor(class)))

enter image description here

if only relative values are needed (as it looks from the provided image) you can scale the values to a 0-1 range:

Dataset1 %>%
  gather(key, value, 1:4) %>%
  group_by(key) %>% #in each key
  mutate(value = scales::rescale(value)) %>% #scale the values
  ggplot()+
  geom_point(aes(x = key, y = value, color = as.factor(class)))

enter image description here

Upvotes: 1

Related Questions