Home > Mobile >  How to present the results of a dataframe in a serial scale using ggplot as in the example attached?
How to present the results of a dataframe in a serial scale using ggplot as in the example attached?

Time:10-11

I have this data frame :

Raw.Score = c(0,1,2,3,4,5,6,7,8)

Severity = c(-3.56553994,-2.70296933,-1.63969850,-0.81321707,-0.04629182,
             0.73721320,1.61278518,2.76647043,3.94804472)

x = data.frame(Raw.Score = Raw.Score, Severity = Severity)

Raw.score are raw numbers from 0 to 8 (let's consider them as the labels of the severity numbers)

Severity are relative numbres that represent the locations of the scores in the diagram

I want to graphically present the results as in the following example using ggplot (the example includes different numbers but I want something similar)

enter image description here

CodePudding user response:

As a fun exercise in ggplot-ing here is one approach to achieve or come close to your desired result.

Raw.Score = c(0,1,2,3,4,5,6,7,8)

Severity = c(-3.56553994,-2.70296933,-1.63969850,-0.81321707,-0.04629182,
             0.73721320,1.61278518,2.76647043,3.94804472)

dat <- data.frame(Raw.Score, Severity)

library(ggplot2)

dat_tile <- data.frame(
  Severity = seq(-4.1, 4.1, .05)
)
dat_axis <- data.frame(
  Severity = seq(-4, 4, 2)
)
tile_height = .15
ymax <- .5

ggplot(dat, aes(y = 0, x = Severity, fill = Severity))  
  geom_hline(yintercept = -tile_height / 2)  
  geom_tile(data = dat_tile, aes(color = Severity), height = tile_height)  
  geom_segment(aes(xend = Severity, yend = -ymax, y = ymax), color = "orange")  
  geom_point(data = dat_axis, 
             y = -tile_height / 2, shape = 21, stroke = 1, fill = "white")  
  geom_text(data = dat_axis, aes(label = Severity),
             y = -tile_height / 2 - .1, vjust = 1, fontface = "bold")  
  geom_label(aes(y = -ymax, label = scales::number(Severity, accuracy = .01)))  
  geom_point(aes(y = ymax, color = Severity), size  = 8)  
  geom_text(aes(y = ymax, label = Raw.Score), fontface = "bold")  
  annotate(geom = "text", fontface = "bold", label = "MILD", color = "black", x = -3.75, y = 0)  
  annotate(geom = "text", fontface = "bold", label = "SEVERE", color = "white", x = 3.75, y = 0)  
  scale_x_continuous(expand = c(0, 0))  
  scale_y_continuous(limits = c(-ymax, ymax))  
  scale_fill_gradient(low = "orange", high = "red", guide = "none")  
  scale_color_gradient(low = "orange", high = "red", guide = "none")  
  theme_void()  
  theme(plot.margin = rep(unit(10, "pt"), 4))  
  coord_cartesian(clip = "off")

enter image description here

  • Related